hats

Haskell client for the NATS messaging system https://github.com/kosmoskatten/hats

Latest on Hackage:0.1.0.1

This package is not currently in any snapshots. If you're interested in using it, we recommend adding it to Stackage Nightly. Doing so will make builds more reliable, and allow stackage.org to host generated Haddocks.

MIT licensed by Patrik Sandahl

HATS - Haskell NATS client

Haskell client for the NATS messaging system (see https://nats.io for a general introduction to NATS).

Examples:

This section gives a simple messaging example using this library. The example requires the presence of a NATS server, running on localhost using the default port 4222. If other host or port, adapt the example.

{-# LANGUAGE OverloadedStrings #-}
module Main
    ( main
    ) where

import Network.Nats
import Text.Printf

main :: IO ()
main =
    withNats defaultSettings ["nats://localhost"] $ \nats -> do

       -- Subscribe to the topic "foo".
       (s, q) <- subscribe nats "foo" Nothing

       -- Publish to topic "foo", do not request a reply.
       publish nats "foo" Nothing "Some payload"

       -- Wait for a message, print the message's payload
       msg <- nextMsg q
       printf "Received %s\n" (show $ payload msg)

       -- Unsubscribe from topic "foo".
       unsubscribe nats s Nothing

Beside from the subscription mode where messages, synchronously, are fetched from a queue there is also an asynchronous mode where each request is handled immediately in their own thread.

{-# LANGUAGE OverloadedStrings #-}
module Main
    ( main
    ) where

import Control.Monad
import Data.Maybe
import Network.Nats
import Text.Printf

main :: IO ()
main =
    withNats defaultSettings ["nats://localhost"] $ \nats -> do
       
        -- A simple - asynchronous - help service that will answer
        -- requesters that give a reply topic with "I can help".
        s1 <- subscribeAsync nats "help" Nothing $ \msg -> do
            printf "Help service received: %s\n" (show $ payload msg)
            when (isJust $ replyTo msg) $
                publish nats (fromJust $ replyTo msg) Nothing "I can help"

        -- Subscribe to help replies.
        (s2, q) <- subscribe nats "help.reply" Nothing

        -- Request help.
        publish nats "help" (Just "help.reply") "Please ..."

        -- Wait for reply.
        msg <- nextMsg q
        printf "Received: %s\n" (show $ payload msg)

        -- Unsubscribe.
        unsubscribe nats s1 Nothing
        unsubscribe nats s2 Nothing

Changes

0.1.0.1

  • Made exception handling for exceptions from the network package a little more robust. Exceptions from the network package is just strings, and matching of errors need to be done by string matching. If a string is changing the exception handler can fail. Added substring matching to better handle this case.

  • Fixing a documentation bug.

0.1.0.0

comments powered byDisqus