bucklescript-phx icon indicating copy to clipboard operation
bucklescript-phx copied to clipboard

Add support for transport

Open laibulle opened this issue 8 years ago • 10 comments

Add support for transport parameter to use client with node js.

 let socket = new Socket(`${this.endpoint}/socket`, {transport: W3CWebSocket, params: "mytoken"})

laibulle avatar Oct 18 '17 07:10 laibulle

You mean this ?

jackalcooper avatar Oct 18 '17 07:10 jackalcooper

Yes, exactly ^^. I am quite new on Reason. Could you provide an example with params ?

laibulle avatar Oct 18 '17 07:10 laibulle

This grammar of BuckleScript should give you a appropriate params object:

[%bs.obj { include_current = true ; network_id = 1 } ]

I've never used Reason. It might be a little different in Reason.

jackalcooper avatar Oct 18 '17 07:10 jackalcooper

Also checkout this section: _binding_to_js_objects

jackalcooper avatar Oct 18 '17 07:10 jackalcooper

I tried this but for now it doesn't work.

  let socketUri = url ^ "/socket";
  let params = {
    "params": {
      "userToken": "Test"
    }
  };

  let socket = initSocket socketUri params |> connectSocket;

I get this error

We've found a bug for you!
  /Users/laibulle/Projects/HaumMobile/re/Api.re 68:37-42

  66 ┆ };
  67 ┆
  68 ┆ let socket = initSocket socketUri params |> connectSocket;
  69 ┆ {
  70 ┆   "fetchHomes": fun () => fetchHomes url token

  The function applied to this argument has type
    opts::Js.t 'a? =>
    Js.t
    {. connectionState : string, endPointURL : string,
      flushSendBuffer : Phx_abstract.void, isConnected : bool,
      log : Js.Internal.meth
            [ `Arity_3 (string, string, Phx_abstract.any) ] Phx_abstract.void,
      makeRef : string,
      onClose : Js.Internal.meth
                [ `Arity_1 Phx_abstract.function_ ] Phx_abstract.void,
      onConnClose : Js.Internal.meth
                    [ `Arity_1 Phx_abstract.any ] Phx_abstract.void,
      onConnError : Js.Internal.meth
                    [ `Arity_1 Phx_abstract.any ] Phx_abstract.void,
      onConnMessage : Js.Internal.meth
                      [ `Arity_1 Phx_abstract.any ] Phx_abstract.void,
      onConnOpen : Phx_abstract.void,
      onError : Js.Internal.meth
                [ `Arity_1 Phx_abstract.function_ ] Phx_abstract.void,
      onMessage : Js.Internal.meth
                  [ `Arity_1 Phx_abstract.function_ ] Phx_abstract.void,
      onOpen : Js.Internal.meth
               [ `Arity_1 Phx_abstract.function_ ] Phx_abstract.void,
      protocol : string,
      push : Js.Internal.meth [ `Arity_1 Phx_abstract.any ] Phx_abstract.void,
      remove : Js.Internal.meth [ `Arity_1 Phx_channel.t ] Phx_abstract.void,
      sendHeartbeat : Phx_abstract.void,
      triggerChanError : Phx_abstract.void }
This argument cannot be applied without label

laibulle avatar Oct 18 '17 08:10 laibulle

let params = [%bs.obj { userToken = "Test" } ]

Try this

jackalcooper avatar Oct 18 '17 08:10 jackalcooper

It give me

  We've found a bug for you!
  /Users/laibulle/Projects/HaumMobile/re/Api.re 62:25-46

  60 │ let connect = fun url token => {
  61 │   let socketUri = url ^ "/socket";
  62 │   let params = [%bs.obj { userToken = "Test" } ];
  63 │
  64 │   /*let params = {

  Unbound instance variable userToken

laibulle avatar Oct 18 '17 09:10 laibulle

Looks like in ReasonML it is

let params = [%bs.obj { userToken : "Test" } ];

Please go to discord of ReasonML. There are a lot of people know Reason well.

jackalcooper avatar Oct 18 '17 09:10 jackalcooper

I'm not sure if I this information is pertinent but being new to Reason and Phoenix I had a hell of a time figuring out how to add argument to initSocket beyond the mount point string. Something like this works:

let opts = { "params": { "user_id": 1 } }

let socket =
  initSocket("ws://foo.localhost:4000/socket", ~opts)
  |> connectSocket
  |> putOnClose(() => Js.log("Socket closed"));

The [bs.obj ] part I don't think is necessary. The key is just labeling the argument "opts".

elliottmason avatar Oct 23 '18 18:10 elliottmason

@lleolin that is a syntax sugar in reason I guess.

jackalcooper avatar Oct 24 '18 08:10 jackalcooper