Broadcast to socket without channel

Yes, I do. I saw somewhere a comment that connect is a transport layer and it is not possible to automatically subscribe.

:thinking:

I’ve just tried the approach I’ve described above for phoenix v1.3 and it worked.

I saw somewhere a comment that connect is a transport layer and it is not possible to automatically subscribe.

Can you link me to that comment?

1 Like

I believe yes

Can you please push your code somewhere?

The link is in my previous post.

Not sure that it works
I launched it in dev mode and connected from iOS plus added two logs in connect and in join so the second one never called.

join as in a channel join? It wouldn’t be called with this approach, the messages are pushed directly to the websocket bypassing any channels. Check out the test (which passes, so it works :+1:).

Ah, sorry misunderstood this.
:thinking:

[{socket_pid, []}] = Registry.lookup(Registry.Lalala, "lalala")
[{#PID<0.335.0>, []}]
send(socket_pid, {:socket_push, "asdf", "asdf"})

in dev mode gives me right now

[error] Process #PID<0.335.0> raised an exception
** (FunctionClauseError) no function clause matching in :cowboy_websocket.websocket_opcode/1
    (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:652: :cowboy_websocket.websocket_opcode("asdf")
    (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:699: :cowboy_websocket.websocket_send/2
    (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:618: :cowboy_websocket.handler_call/7
    (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:49: Phoenix.Endpoint.CowboyWebSocket.resume/3
    (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
[error] Ranch protocol #PID<0.335.0> of listener SocketBroadcastWeb.Endpoint.HTTP (cowboy_protocol) terminated
** (exit) an exception was raised: 
    ** (FunctionClauseError) no function clause matching in :cowboy_websocket.websocket_opcode/1
        (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:652: :cowboy_websocket.websocket_opcode("asdf")
        (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:699: :cowboy_websocket.websocket_send/2
        (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_websocket.erl:618: :cowboy_websocket.handler_call/7
        (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:49: Phoenix.Endpoint.CowboyWebSocket.resume/3
        (cowboy) /Users/vlad/Documents/elixir/github/socket_push/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

You need to use proper values instead of "asdf" for what you actually want to send to the client.

For example

send(socket_pid, {:socket_push, :text, Poison.encode!(%{"oh" => "my"})})
2 Likes