How to send messages to a channel without JS, using something like websocat

So I’d like to test building an app that can interact with websockets over the command line. I have been able to connect to a server using websocat websocat "ws://127.0.0.1:4000/socket/websocket?token=token&vsn=2.0.0". I even figured out how to join a topic by sending [null, null, "room:lobby", "phx_join", {}]. However I don’t quite understand what the two null values represent. When I try to send something as JSON, I get the following error:

[error] Ranch listener ChanWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.479.0> exit with reason: {{:badmatch, "{\"event\":\"phx_join\",\"payload\":{},\"ref\":null,\"topic\":\"room:lobby\"}"}, [{Phoenix.Socket.V2.JSONSerializer, :decode!, 2, [file: 'lib/phoenix/socket/serializers/v2_json_serializer.ex', line: 30]}, {Phoenix.Socket, :__in__, 2, [file: 'lib/phoenix/socket.ex', line: 583]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 120]}, {:cowboy_websocket, :handler_call, ...

How can I send a message that would be handled by a handle_info/2 callback? Where are these things documented?

The two null values are the join ref and message ref, they are numbers that Phoenix uses to relate a request/response to each other, and to know that nothing has gone wrong with the connection (loosely said). They are documented in the Phoenix message struct, and the serialization format (V2) can be found in code (https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/socket/serializers/v2_json_serializer.ex#L30).

What payload are you sending to cause the match error to appear?

Fwiw, we use wscat in the Real-Time Phoenix book to test a Phoenix Channels app from the command line.

2 Likes

Interesting. I’ll have to check out that book. I didn’t know about it. How much of it is available in the beta?

The book is content complete, just a few bits of editing left I believe.

1 Like

Correct. Just buttoning things up now.

Just wanted to add that I purchased the above book and am glad I did, it’s fantastic and explains things very well. Fantastic book.

1 Like