hats: Haskell client for the NATS messaging system

[ library, mit, network, program ] [ Propose Tags ]

A Haskell client for the NATS messaging system. To get started, see the documentation for the Network.Nats module. Or see the example programs in the example directory.

A general introduction to NATS can be found at https://nats.io.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1
Change log CHANGES.md
Dependencies aeson, async, attoparsec, base (>=4.7 && <5), bytestring, conduit, conduit-extra, connection, deepseq, hats, network, network-uri, random, stm, text, transformers, unordered-containers [details]
License MIT
Copyright 2016 Patrik Sandahl
Author Patrik Sandahl
Maintainer patrik.sandahl@gmail.com
Category Network
Home page https://github.com/kosmoskatten/hats
Bug tracker https://github.com/kosmoskatten/hats/issues
Source repo head: git clone https://github.com/kosmoskatten/hats
Uploaded by PatrikSandahl at 2016-10-04T20:32:37Z
Distributions
Executables hats-examples
Downloads 1308 total (6 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-10-04 [all 1 reports]

Readme for hats-0.1.0.1

[back to package description]

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