module Network.Nats.Json (
subscribe
, publish
, requestMany
) where
import Control.Applicative ((<$>))
import qualified Data.Aeson as AE
import Data.Maybe (mapMaybe)
import Network.Nats (Nats, NatsSID)
import qualified Network.Nats as N
publish :: AE.ToJSON a =>
Nats
-> String
-> a
-> IO ()
publish nats subject body = N.publish nats subject (AE.encode body)
subscribe :: AE.FromJSON a =>
Nats
-> String
-> Maybe String
-> (NatsSID
-> String
-> a
-> Maybe String
-> IO ()
)
-> IO NatsSID
subscribe nats subject queue jcallback = N.subscribe nats subject queue cb
where
cb sid subj msg repl = case AE.eitherDecode msg of
Right body -> jcallback sid subj body repl
Left err -> putStrLn $ err ++ ": " ++ show msg
requestMany :: (AE.ToJSON a, AE.FromJSON b) =>
Nats
-> String
-> a
-> Int
-> IO [b]
requestMany nats subject body time =
decodeAndFilter <$> N.requestMany nats subject (AE.encode body) time
where
decodeAndFilter = mapMaybe AE.decode