The hats package

[ Tags: benchmark, 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]

Properties

Versions 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 repository head: git clone https://github.com/kosmoskatten/hats
Uploaded Tue Oct 4 20:32:37 UTC 2016 by PatrikSandahl
Distributions NixOS:0.1.0.1
Executables hats-examples
Downloads 111 total (4 in the last 30 days)
Rating 0.0 (0 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-10-04 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


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