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

Broadcast a message #3

Open
alex88 opened this issue Jul 13, 2017 · 8 comments
Open

Broadcast a message #3

alex88 opened this issue Jul 13, 2017 · 8 comments

Comments

@alex88
Copy link

alex88 commented Jul 13, 2017

In the Readme under Broadcast from elsewhere you use Websockets but that's not available, what should I use?

I've seen the broadcast function is in https://hexdocs.pm/web_socket/WebSocket.Events.html#content but what should ref be?

@alex88
Copy link
Author

alex88 commented Jul 13, 2017

Oh nvm seems it's just the handle I se in the router, sorry :)

@alex88 alex88 closed this as completed Jul 13, 2017
@alex88
Copy link
Author

alex88 commented Jul 13, 2017

Seems still not working, this is my code:

router

  socket "/socket", MyApp.WebsocketController, :handle

  def start_link do
    Logger.info("Starting http server")
    Plug.Adapters.Cowboy.http(__MODULE__, [], port: 4000 dispatch: dispatch_table())
  end

handler

defmodule MyApp.WebsocketController do
  def handle(:init, state) do
    {:ok, %{state | use_topics: false}}
  end

  def handle(:terminate, _state) do
    :ok
  end

  def handle(message, state) do
    {:reply, message, state}
  end
end

in my code I do

    message = WebSocket.Message.build("data", data_object)
    json = Poison.encode!(message)
    WebSocket.Events.broadcast!(:handle, json)

but nothing gets received on the websocket client browser

@alex88 alex88 reopened this Jul 13, 2017
@slogsdon
Copy link
Owner

slogsdon commented Jul 14, 2017

Thanks for the report, @alex88! Looks like the README needs some updating :)

What's your client code look like? Are there any errors displayed in your application's console?

To be honest, there's a good chance the library needs updating. I haven't really messed with it for 2 years, outside of updating some deps. The tests pass, but they're definitely not comprehensive. It also looks like only 1/2 of the examples are working, with the "Topics" example being the one that isn't working. When running the "Topics" example, I'm seeing the following error which closes the open connection:

21:08:42.471 [error] Process #PID<0.300.0> raised an exception
** (ErlangError) erlang error: [reason: {:badkey, :event, %{"data" => "WebSocket rocks", "event" => "topic:a"}}, mfa: {WebSocket.Cowboy.Handler, :websocket_handle, 3}, stacktrace: [{WebSocket.Cowboy.Handler, :get_args, 2, [file: 'lib/web_socket/cowboy/handler.ex', line: 160]}, {WebSocket.Cowboy.Handler, :websocket_handle, 3, [file: 'lib/web_socket/cowboy/handler.ex', line: 98]}, {:cowboy_websocket, :handler_call, 7, [file: 'src/cowboy_websocket.erl', line: 588]}, {:cowboy_protocol, :execute, 4, [file: 'src/cowboy_protocol.erl', line: 442]}], msg: {:text, "{\"event\":\"topic:a\",\"data\":\"WebSocket rocks\"}"}, req: [socket: #Port<0.6875>, transport: :ranch_tcp, connection: :keepalive, pid: #PID<0.300.0>, method: "GET", version: :"HTTP/1.1", peer: {{127, 0, 0, 1}, 51972}, host: "localhost", host_info: :undefined, port: 4000, path: "/topic", path_info: :undefined, qs: "", qs_vals: :undefined, bindings: [], headers: [{"upgrade", "websocket"}, {"connection", "Upgrade"}, {"host", "localhost:4000"}, {"origin", "http://localhost:4000"}, {"pragma", "no-cache"}, {"cache-control", "no-cache"}, {"sec-websocket-key", "VGivwKe/ASv7UfD7JGdk5g=="}, {"sec-websocket-version", "13"}, {"sec-websocket-extensions", "x-webkit-deflate-frame"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/603.2.5 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.5"}], p_headers: [{"sec-websocket-extensions", [{"x-webkit-deflate-frame", []}]}, {"upgrade", ["websocket"]}, {"connection", ["upgrade"]}], cookies: :undefined, meta: [websocket_version: 13, websocket_compress: false], body_state: :waiting, buffer: "", multipart: :undefined, resp_compress: false, resp_state: :done, resp_headers: [], resp_body: "", onresponse: :undefined], state: %WebSocket.Cowboy.Handler.State{action: :handle, conn: %Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [], body_params: %Plug.Conn.Unfetched{aspect: :body_params}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.300.0>, params: %Plug.Conn.Unfetched{aspect: :params}, path_info: ["topic"], peer: {{127, 0, 0, 1}, 51972}, port: 4000, private: %{}, query_params: %Plug.Conn.Unfetched{aspect: :query_params}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"upgrade", "websocket"}, {"connection", "Upgrade"}, {"host", "localhost:4000"}, {"origin", "http://localhost:4000"}, {"pragma", "no-cache"}, {"cache-control", "no-cache"}, {"sec-websocket-key", "VGivwKe/ASv7UfD7JGdk5g=="}, {"sec-websocket-version", "13"}, {"sec-websocket-extensions", "x-webkit-deflate-frame"}, {"user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/603.2.5 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.5"}], request_path: "/topic", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}], scheme: :ws, script_name: [], secret_key_base: nil, state: :unset, status: nil}, plug: TopicController, use_topics: true}]
    (cowboy) src/cowboy_websocket.erl:642: :cowboy_websocket.handler_call/7
    (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

While I'm not 100% sure yet, I have an idea about what is causing this issue. I'll try to get this fixed as soon as I can.

@alex88
Copy link
Author

alex88 commented Jul 14, 2017

No worries @slogsdon, unfortunately I had to switch to Phoenix just for the websocket feature (deadlines can't wait 😞 )

On the client side I was just using new Websocket(url) and then the regular onmessage, onclose callbacks.
However when I was saying that I didn't receive any message, I was looking in the chrome devtools in the websocket connection's frames tab, so unless there's something messed up with topics (which should be disabled and honestly I don't know how they work) I don't know why it wasn't working.
I've checked the Websocket.Events code and the maybe_send is actually sending the message but I haven't debugged further than that!

@slogsdon
Copy link
Owner

@alex88 Looks like it was easier than I thought. There was a change in Poison's decode functionality that changed JSON keys from atoms to strings, so it was interfering with how I was trying to use that data. I've pushed a fix via 1fcec4c, but I'm currently looking to find a nice way to write some integration tests to cover this and the rest of the library.

@slogsdon
Copy link
Owner

As a note, I had intended on shipping this library with this JavaScript helper to aid in the message format for topic support, which may or may not be of use if you end up coming back from Phoenix. At the current state, the innards of the library expects the raw message sent between the server and client to contain an event property to contain topic information and a data property to contain any application data for the event.

@alex88
Copy link
Author

alex88 commented Jul 14, 2017

With your current js client you mean? Because from the code seems anything would have been ok

@slogsdon
Copy link
Owner

slogsdon commented Jul 14, 2017

Yeah, anything would be valid at this current state on both the client and server with any event and/or message. I was only bringing the client library up for the thin wrapper it provides now. The endgame for this library (if it gets to that point) is to provide some conventions for both server and client message formats where the default acts as it does today.

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

2 participants