niho / elm-stomp / Stomp.Client

A session manages the connection with the server and is used to send commands to the server.

Ports


type alias Connection msg =
Stomp.Internal.Connection.Connection msg

Connection to a STOMP server. This is the type of the outgoing port.

port socket : Stomp.Client.Connection msg


type alias OnMessage msg =
Stomp.Internal.Connection.OnMessage msg

Incoming port for messages from the STOMP server.

port onMessage : Stomp.Client.OnMessage msg

listen : OnMessage msg -> Session msg -> Platform.Sub.Sub msg

A subscription that will listen for new WebSocket messages on the provided port and dispatch them to the STOMP session.

subscriptions : State -> Sub Msg
subscriptions state =
    Stomp.Client.listen onMessage state.session

Session


type alias Options msg =
Stomp.Internal.Session.Options msg

The messages to send to your update function when the state of the connection changes.

{ onConnected : msg
, onDisconnected : msg
, onError : String -> msg
, onHeartBeat : msg
}


type alias Session msg =
Stomp.Internal.Session.Session msg

The Session holds the state of the connection.

init : Connection msg -> Options msg -> Session msg

Create a session that manages the WebSocket connection (through a port) and keeps track of the STOMP state.

Stomp.Client.init socket
    { onConnected = Connected
    , onDisconnected = Disconnected
    , onError = Error
    , onHeartBeat = HeartBeat
    }

Connection

connect : Session msg -> String -> String -> String -> Platform.Cmd.Cmd msg

Send a CONNECT frame to the server. This always needs to be the first message sent, to establish the connection and authenticate the user. Trying to send a message before the server has recieved and acknowledged the CONNECT message will result in the server simply closing the socket.

Stomp.Client.connect session "guest" "guest" "/"

disconnect : Session msg -> ( Session msg, Platform.Cmd.Cmd msg )

Send a DISCONNECT frame to the server.

Commands

send : Session msg -> String -> List Stomp.Internal.Frame.Header -> Stomp.Internal.Body.Value -> ( Session msg, Platform.Cmd.Cmd msg )

Send a message to a specific topic.

sendStrings : List String -> Cmd Msg
sendStrings strings =
    let
        topic =
            "example.strings"

        headers =
            [ ( "x-example", "this is a header" ) ]

        body =
            Json.Encode.list (List.map Json.Encode.string strings)
    in
    Stomp.Session.send session topic headers body

call : Session msg -> Stomp.Proc.RemoteProcedure msg -> ( Session msg, Platform.Cmd.Cmd msg )

Send a remote procedure call to a server.

type Msg
    = Strings (Result String Stomp.Message.Message)

getStrings : Cmd Msg
getStrings =
    Stomp.Proc.init "example.strings"
        |> Stomp.Proc.onResponse Strings
        |> Stomp.Session.call session

subscribe : Session msg -> Stomp.Subscription.Subscription msg -> ( Session msg, Platform.Cmd.Cmd msg )

Subscribe to message from a server on a specific topic.

type Msg
    = Strings (Result String Stomp.Message.Message)

strings : Cmd Msg
strings =
    Stomp.Subscription.init "example.strings"
        |> Stomp.Subscription.onMessage Strings
        |> Stomp.Session.subscribe session

unsubscribe : Session msg -> Stomp.Subscription.Subscription msg -> ( Session msg, Platform.Cmd.Cmd msg )

Unsubscribe an existing subscription (uses subscription id to identify which subscription to unsubscribe).

Stomp.Subscription.init "example.strings"
    |> Stomp.Subscription.withSubscriptionId "strings-1"
    |> Stomp.Session.unsubscribe session

Acknowledgement

ack : Session msg -> Stomp.Message.Message -> Maybe String -> ( Session msg, Platform.Cmd.Cmd msg )

Acknowledge that a message was consumed by the session when using SessionAck or SessionIndividualAck modes on a subscription.

nack : Session msg -> Stomp.Message.Message -> Maybe String -> ( Session msg, Platform.Cmd.Cmd msg )

The opposite of ack.

It is used to tell the server that the session did not consume the message. The server can then either send the message to a different session, discard it, or put it in a dead letter queue. The exact behavior is server specific.

nack applies either to one single message (if the subscription's ack mode is SessionIndividualAck) or to all messages sent before and not yet ack'ed or nack'ed (if the subscription's ack mode is SessionAck).

Transactions

begin : Session msg -> String -> ( Session msg, Platform.Cmd.Cmd msg )

begin is used to start a transaction. Transactions in this case apply to sending and acknowledging - any messages sent or acknowledged during a transaction will be processed atomically based on the transaction.

commit : Session msg -> String -> ( Session msg, Platform.Cmd.Cmd msg )

commit is used to commit a transaction in progress.

abort : Session msg -> String -> ( Session msg, Platform.Cmd.Cmd msg )

abort is used to roll back a transaction in progress.