@hackage freddy0.1.1.0

RabbitMQ Messaging API supporting request-response

RabbitMQ Messaging API supporting request-response

Build Status Code Climate

Setup

  • Create a connection:
import qualified Network.Freddy as Freddy
import qualified Network.Freddy.Request as R

connection <- Freddy.connect "127.0.0.1" "/" "guest" "guest"

Delivering messages

Simple delivery

Send and forget

Sends a message to the given destination. If there is no consumer then the message stays in the queue until somebody consumes it.

  Freddy.deliver connection R.newReq {
    R.queueName = "notifications.user_signed_in",
    R.body = "{\"user_id\": 1}"
  }

Expiring messages

Sends a message to the given destination. If nobody consumes the message in timeoutInMs milliseconds then the message is discarded. This is useful for showing notifications that must happen in a certain timeframe but guaranteed delivery is not a strict requirement.

  Freddy.deliver connection R.newReq {
    R.queueName = "notifications.user_signed_in",
    R.body = "{\"user_id\": 1}",
    R.timeoutIsMs = 5000
  }

Request delivery

Sends a message to the given destination. Has a default timeout of 3 seconds and discards the message from the queue if a response hasn't been returned in that time.

  response <- Freddy.deliverWithResponse connection R.newReq {
    R.queueName = "echo",
    R.body = "{\"msg\": \"what did you say?\"}"
  }

  case response of
    Right payload -> putStrLn "Received positive result"
    Left (Freddy.InvalidRequest payload) -> putStrLn "Received error"
    Left Freddy.TimeoutError -> putStrLn "Request timed out"

Responding to messages

  processMessage (Freddy.Delivery body replyWith failWith) = replyWith body

  connection <- Freddy.connect "127.0.0.1" "/" "guest" "guest"
  Freddy.respondTo connection "echo" processMessage

Tapping into messages

Listens for messages on a given destination or destinations without consuming them.

  processMessage body = putStrLn body

  connection <- Freddy.connect "127.0.0.1" "/" "guest" "guest"
  Freddy.tapInto connection "notifications.*" processMessage
  • Note that it is not possible to respond to the message while tapping.
  • When tapping the following wildcards are supported in the queueName:
    • # matching 0 or more words
    • * matching exactly one word

Examples:

Freddy.tapInto connection "i.#.free" processMessage

receives messages that are delivered to "i.want.to.break.free"

Freddy.tapInto connection "somebody.*.love" processMessage

receives messages that are delivered to somebody.to.love but doesn't receive messages delivered to someboy.not.to.love