bucklescript-tea
bucklescript-tea copied to clipboard
websocket subscription
The following statement successfully sets up a connection to a websocket server
let ws = WebSockets.WebSocket.make "ws://localhost:5858"
|> WebSockets.WebSocket.setBinaryType ArrayBuffer
|> WebSockets.WebSocket.on @@ Open
(fun _ -> Js.log "Open")
|> WebSockets.WebSocket.on @@ Close
(fun _ -> Js.log "Close")
|> WebSockets.WebSocket.on @@ Message
on_message
|> WebSockets.WebSocket.on @@ Error
(fun _ -> Js.log "error")
;;
How can bs-tea easily subscribe to on_message ?
Could we just manually trigger an update in on_message
by using some internal function in Vdom.ml
?
I do understand that such an approach might be fundamentally different from traditional TEA ---
However said approach is more logically sensible to me and allows for much more fine-grained tuning & control over the server
You can checkout the source code of Random
to get an idea how to enqueue a new message with a mapper passed in. Like this:
let generate tagger (Generator genCmd) =
Tea_cmd.call (fun callbacks ->
let state = Random.get_state () in
let genValue = genCmd state in
let () = Random.set_state state in
let open Vdom in
!callbacks.enqueue (tagger genValue)
)
You don't really need Elm's subscription because BuckleScript make it really easily to enqueue a message when a callback of JavaScript is called. If you want to do it you can check out the source of Mouse
.
@OvermindDL1 maybe we should add an example for this in README? It is definitely a good point to sell comparing to original Elm.
@OvermindDL1 maybe we should add an example for this in README? It is definitely a good point to sell comparing to original Elm.
Very true, websockets will be significantly easier to pull off in TEA than Elm. I'll leave this open until I (or a PR) gets to it. :-)
But yeah, making new subscriptions and commands both are really easy in TEA unlike in Elm (where you cannot make new ones without an effect manager, which is highly limited as well). :-) Tea just uses bog-standard callbacks. ^.^
Also, for note, the !callbacks.enqueue
is equivalent to Elm's Router
concept, just via a typed function call instead of a full unique type. :-)
I have some examples with phoenix websockets that I could clean up a bit. What are we looking for exactly?
On Wed, 1 Nov 2017, 15:40 OvermindDL1, [email protected] wrote:
Also, for note, the !callbacks.enqueue is equivalent to Elm's Router concept, just via a typed function call instead of a full unique type. :-)
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/OvermindDL1/bucklescript-tea/issues/58#issuecomment-341125572, or mute the thread https://github.com/notifications/unsubscribe-auth/AACx6gC9bAvYmOXnAvPLNlqbBIw8XmZcks5syILKgaJpZM4QNT_J .
Probably need a websocket subscription manager written overall. Should not be too hard, I can do it when I get time again (so mid-late November for me), but if you want to do it feel free too. :-)
As our intuition suggests -- the subscription aspect of TEA is not necessary in OCaml-Tea
Precisely. :-)
It is still usually good form, but absolutely not required. :-)
You don't really need the Cmd in your on_message as it is not doing anything for note. :-)
I'm going to leave this open until I do get a subscription manager set up for it though, to remind me. :-)
I thought the previous attempt of using Tea.Cmd.call
was a solution ... but alas update does not get called.
I was also looking into Tea_task.peform
but that does not seem to do anything either.
So does anyone have an idea on how to manually trigger an update ?
I thought the previous attempt of using Tea.Cmd.call was a solution ... but alas update does not get called.
You have to return the results of that in the return of the update callback for it to be executed.
I was also looking into Tea_task.peform but that does not seem to do anything either.
Ditto, needs to be returned in a command.
Remember, only immutable things should happen inside all callbacks. Any 'actions' must be returned so the runtime system can execute them.