orus-io / elm-nats / Nats.Protocol

Provides types and utilities for the NATS protocol


type Operation datatype
    = INFO ServerInfo
    | CONNECT ConnectOptions
    | PUB (Message datatype)
    | SUB String String String
    | UNSUB String Basics.Int
    | MSG String (Message datatype)
    | PING
    | PONG
    | OK
    | ERR String

Typed operations of the NATS protocol

Here is the protocol documentation:

CONNECT Client Sent to server to specify connection information
PUB Client Publish a message to a subject, with optional reply subject
SUB Client Subscribe to a subject (or subject wildcard)
UNSUB Client Unsubscribe (or auto-unsubscribe) from subject
MSG Server Delivers a message payload to a subscriber
PING Both PING keep-alive message
PONG Both PONG keep-alive response
+OK Server Acknowledges well-formed protocol message in verbose mode
-ERR Server Indicates a protocol error. Will cause client disconnect.


type alias Message datatype =
{ subject : String
, replyTo : String
, size : Basics.Int
, data : datatype 
}

A NATS message


type alias ServerInfo =
{ server_id : String
, version : String
, go : String
, host : String
, port_ : Basics.Int
, auth_required : Basics.Bool
, max_payload : Basics.Int 
}

Information sent by the server immediately after opening the connection


type alias ConnectOptions =
{ verbose : Basics.Bool
, pedantic : Basics.Bool
, auth_token : Maybe String
, user : Maybe String
, pass : Maybe String
, name : Maybe String
, lang : String
, version : String
, protocol : Basics.Int 
}

Options for the CONNECT operation

parseString : ParseState String -> String -> ParseResult String

Parse an operation (generally received from the server) from a string

Any message contained will be of type 'String'

parseBytes : ParseState Bytes -> Bytes -> ParseResult Bytes

Parse an operation from binary data.

Any message contained will be of type 'Bytes'

toBytes : Operation Bytes -> Bytes

serialize to Bytes

toString : Operation String -> String

serialize an Operation (generally for sending to the server)

if the operation contains data as bytes, the result string will be base64 encoded


type alias ParseResult datatype =
Result String ( List (Operation datatype)
, ParseState datatype 
}

The result of parsing an operation


type ParseState datatype

A temporary state when a parsing an operation is incomplete

initialParseState : ParseState datatype

Returns a initial ParseState