Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket subscription #58

Open
iams-wu opened this issue Oct 31, 2017 · 10 comments
Open

websocket subscription #58

iams-wu opened this issue Oct 31, 2017 · 10 comments

Comments

@iams-wu
Copy link

iams-wu commented Oct 31, 2017

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

@jackalcooper
Copy link
Contributor

jackalcooper commented Nov 1, 2017

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.

@jackalcooper
Copy link
Contributor

jackalcooper commented Nov 1, 2017

@OvermindDL1 maybe we should add an example for this in README? It is definitely a good point to sell comparing to original Elm.

@OvermindDL1
Copy link
Owner

@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. ^.^

@OvermindDL1
Copy link
Owner

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. :-)

@tcoopman
Copy link
Contributor

tcoopman commented Nov 1, 2017 via email

@OvermindDL1
Copy link
Owner

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. :-)

@iams-wu iams-wu closed this as completed Nov 1, 2017
@OvermindDL1
Copy link
Owner

OvermindDL1 commented Nov 1, 2017

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. :-)

@OvermindDL1
Copy link
Owner

I'm going to leave this open until I do get a subscription manager set up for it though, to remind me. :-)

@OvermindDL1 OvermindDL1 reopened this Nov 1, 2017
@iams-wu
Copy link
Author

iams-wu commented Nov 13, 2017

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 ?

@OvermindDL1
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants