haskell-mqtt icon indicating copy to clipboard operation
haskell-mqtt copied to clipboard

Possibility to modify message on publish up/downstream callbacks.

Open ibnuda opened this issue 6 years ago • 0 comments

Hello,

At this line, you have decided that the callbacks' signatures on onPublishUpstreamMessage and onPublishDownstreamMessage as Message -> IO ()s. Is it possible to create pull request to change it into Message -> Message?

So on this line, instead of

publishUpstream :: Broker auth -> Message -> IO ()
publishUpstream broker msg = do
  onPublishUpstream (brokerCallbacks broker) msg
  publishDownstream broker msg

the library use:

publishUpstream :: Broker auth -> Message -> IO Message
publishUpstream broker msg = 
  onPublishUpstream (brokerCallbacks broker) msg >>= publishDownstream broker

There's a possibility that in a custom broker, the user wants to modify the message that being passed around because of some reasons.

For an example, in hummingbird' Internal.hs, instead of

, Broker.onPublishUpstream   = \_->Prometheus.incCounter (Prometheus.hummingbird_publications_upstream_total metrics)

One could use

, Broker.onPublishUpstream  msg = do
    Prometheus.incCounter (Prometheus.hummingbird_publications_upstream_total metrics)
    pure msg

Or when user wants to modify the message

, Broker.onPublishUpstream  msg = pure $ msg { msgTopic = "some/other/topic" }

There are a few trade-offs here: Pros:

  • A little more flexible library.

Cons:

  • Added computation process.
  • The above computation could be abused by adding too many computation so this library's (and/or custom broker's) performance would drop a little (or much depends on the load).

ibnuda avatar Mar 23 '18 07:03 ibnuda