orus-io / elm-nats / Nats

A nats.io client for Elm

socket

connect : Protocol.ConnectOptions -> Socket -> (Events.SocketEvent -> msg) -> Sub datatype msg

Open a socket

The socket will be opened as soon as the subscription is active, and until the subscriptions is removed.

The socket may get closed if a network error occurs, in which case a 'SocketError' event will be sent

After the subscription is removed, the handler will receive a SocketClosed event.

pub/sub/request

publish : String -> datatype -> Effect datatype msg

Publish a new message on a given subject

If you wish to send it on a non-default socket, use Nats.Effect.onSocket

publishRequest : String -> String -> datatype -> Effect datatype msg

Publish a new message on a given subject, with a 'replyTo' attribute

subscribe : String -> (Protocol.Message datatype -> msg) -> Sub datatype msg

Subscribe to a subject

If you wish to subscribe on a non-default socket, use Nats.Sub.onSocket

groupSubscribe : String -> String -> (Protocol.Message datatype -> msg) -> Sub datatype msg

Subscribe to a subject with a group

If you wish to subscribe on a non-default socket, use Nats.Sub.onSocket

request : String -> datatype -> (Result Errors.Timeout datatype -> msg) -> Effect datatype msg

Create a request

The timeout is 5s by default

requestWithTimeout : Basics.Int -> String -> datatype -> (Result Errors.Timeout datatype -> msg) -> Effect datatype msg

Create a request with a custom timeout

customRequest : { marker : String, subject : String, replyTo : Maybe String, message : datatype, onTimeout : Errors.Timeout -> msg, onResponse : Protocol.Message datatype -> ( Maybe msg, Basics.Bool ), timeout : Maybe Basics.Int } -> Effect datatype msg

Create a request with custom features

track : String -> Sub datatype msg

Track a request

cancelRequest : String -> Effect datatype msg

Cancel an active request, given its marker

withMarker : String -> Effect datatype msg -> Effect datatype msg

Add a marker on a request

If the effect is not a request, the function is no-op

Types


type alias Config datatype portdatatype msg =
Internal.Types.Config datatype portdatatype msg

The NATS configuration


type State datatype msg

The nats internal state


type alias Msg datatype msg =
Internal.Types.Msg datatype msg

A nats internal Msg

withInboxPrefix : String -> State datatype msg -> State datatype msg

Set a custom inbox prefix

Effects


type alias Effect datatype msg =
Internal.Types.Effect datatype msg

A nats effect

Kind of like Cmd, but will be converted at the last moment to regular Cmd


type alias Sub datatype msg =
Internal.Sub.Sub datatype msg

A nats subscription

Will be converted at the last moment to regular Sub.

applyEffectAndSub : Config datatype portdatatype msg -> Effect datatype msg -> Sub datatype msg -> State datatype msg -> ( State datatype msg, Platform.Cmd.Cmd msg )

Update the nats state according to all the Nats.Effect and Nats.Sub gathered by the app root component, and emit all the necessary Cmd

...

init : Random.Seed -> Time.Posix -> State datatype msg

Initialise a new nats state

update : Config datatype portdatatype msg -> Msg datatype msg -> State datatype msg -> ( State datatype msg, Platform.Cmd.Cmd msg )

Handle Nats Msg

subscriptions : Config datatype portdatatype msg -> State datatype msg -> Platform.Sub.Sub msg

Connect the nats internal state to the ports

activeRequests : State datatype msg -> List { sid : String, id : String, marker : Maybe String, subject : String, inbox : String }

List all the active requests