for more information visit the package's GitHub page
Package contains the following modules:
NATS is a simple, secure and performant communications system for digital systems, services and devices.
This library is a pure Elm implementation of the NATS client protocol on top a websocket.
A running nats-server, with websockets enabled (see WebSocket Configuration Example)
go install github.com/nats-io/nats-server/v2@latest
cd examples/string_socket
nats-server -c server.conf
cd examples/string_socket
elm-go -- src/Main.elm -o main.js
NATS subscriptions handling is a stateful business, which is all done by the Nats module. You need to wire it into your application.
Import the Nats module
elm
import Nats
import Nats.Config
import Nats.Effect
import Nats.PortsAPI
import Nats.Protocol
import Nats.Socket
import Nats.Sub
Add a global nats state, on your top-level application model, or Shared model. At this point you need to choose between Bytes and String sockets. The String socket will only be able to exchange String messages with the server.
Using protobuf requires a Bytes socket.
elm
type alias Model =
{ nats: Nats.State String Msg
-- ...
}
elm
type Msg
= ...
| NatsMsg Nats.Msg
```elm
port natsSend : Nats.PortsAPI.Send String msg port natsReceive : Nats.PortsAPI.Receive String msg
natsConfig : Nats.Config String String Msg natsConfig = Nats.Config.string NatsMsg { send = natsSend , receive = natsReceive } ```
elm
init { flags | now : Int } =
{ nats = Nats.init now (Time.millisToPosix now)
}
elm
subscriptions : Model -> Sub Msg
subscriptions =
Nats.subscriptions natsConfig model.nats
elm
natsSubscriptions : Model -> Nats.Sub
natsSubscriptions model =
Nats.Sub.none
```elm wrappedUpdate : Msg -> Model -> ( Model, Cmd Msg ) wrappedUpdate msg model = let ( newModel, natsEffect, cmd ) = update msg model
( nats, natsCmd ) =
Nats.applyEffectAndSub natsConfig natsEffect (natsSubscriptions model) newModel.nats
in
( { newModel | nats = nats }
, Cmd.batch [ cmd, natsCmd ]
)
update : Msg -> Model -> ( Model, Nats.Effect Msg, Cmd Msg) update msg model = ( model, Nats.Effect.none, Cmd.none ) ```
html
<html>
<head>
<meta charset="UTF-8">
<script src="main.js"></script>
<script src="natsports.js"></script>
</head>
<body>
<script>
var app = Elm.Main.init( { flags: { now: Date.now() } } );
setupNatsPorts(app);
</script>
</body>
</html>
Connect a socket is done with simple subscription:
natsSubscriptions : Model -> Nats.Sub
natsSubscriptions model =
Nats.connect
( Nats.Socket.connectOptions
|> Nats.Socket.withUserPass "user" "password"
)
( Nats.Socket.new "0" "ws://localhost:8087" )
The socket definition and connection options will generally be saved on the model
In update, use publish to generate the right Effect:
( model
, Nats.publish "subject1" "Hello world!"
, Cmd.none
)
```elm
type Msg = ReceiveSubject1 (Nats.Protocol.Message String) ```
elm
natsSubscriptions model =
Nats.subscribe "subject1" ReceiveSubject1
```elm
type Msg = ReceiveResponse (Result Nats.Errors.Timeout (Nats.Protocol.Message String)) ```
elm
( model
, Nats.request "subject1" "message" ReceiveResponse
, Cmd.none
)