The easiest way of using this library is by using the airlock template.
This is for setting up this library from scratch.
Urbit subscriptions require server-side event (SSE). Unfortunately, Elm does not provide a native implementation. This means that we will need to use JS FFI (aka ports).
You would need to declare two ports for Airlock to use:
-
port createEventSource : String -> Cmd msg
This sets up the SSE channel.
-
port onEventSourceMessage : (JD.Value -> msg) -> Sub msg
This recieves the actual messages from the SSE channel.
These two functions need to be passed to one of the functions from Ur.Run
module.
The functions in Ur.Run
are the same as functions from Browser
, but have a couple of extra urbit-specific fields.
The SSE JS code is neatly packed into example/fetch-event-source.js. All you need to do is pass them to Elm ports like in example/script.js.
You can see a full working example in example.