pejrich

pejrich

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?

Most Liked

sb8244

sb8244

Author of Real-Time Phoenix

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.

trarbr

trarbr

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

joshtaylor

joshtaylor

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.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement