quda
Phoenix Channels (ws) without frontend
I am trying to develop a small websocket API (as a testbed first) but without using the Phoenix frontend (that socket.js). I need it to communicate to any ws client, not related to Elixir/Phoenix, just exchanging json messages.
Of course I started from the standard, ubiquitous tutorial found online, that one with a simple chat, by modifying it:
- setup Phoenix project – standard install/config, then:
mix phx.gen.channel room - started & tested the Phoenix server installation in the browser – running fine
- modified endpoint file:
socket "/socket", TestWeb.UserSocket,
websocket: true,
longpoll : false
- user_socket.ex file added in:
channel("room:*", TestWeb.RoomChannel) - All others left default/unmodified
As you see, nothing special…
Further on, as I understood that the channel must work out-of-the-box reply by default to join, I tried to connect with wscat: wscat -c ws://localhost:4000/socket
Result:
error: Unexpected server response: 404
Hmm… something wrong with the url then tried also with: ws://localhost:4000/socket/room and ws://localhost:4000/room… and several other combinations. But got the same result : 404 ![]()
Maybe wscat is faulty… another series of trial connections using another websocket client, but with similar results.
On the server side, Elixir reports:
[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /socket (TestWeb.Router)
(test 0.1.0) lib/phoenix/router.ex:406: TestWeb.Router.call/2
...
Obviously, I’m missing something trivial but fundamental here, but I can’t figure out what it is.
Please help.
Most Liked
chrismccord
@LostKobrakai covered the details perfectly here in how channels work and what’s required. Any client can talk to channels if they can write bits on the wire, and elixir clients exist in the community. Here’s a great one that you can drop in and work with:
LostKobrakai
Phoenix channels are “phoenix channels” not “arbitrary websocket messages”. You can use any websocket client you want, but you’ll need to adhere to the message protocol expected by the server as well as lifecycle messages needed.
LostKobrakai
@quda here you can find an example, which proxies iex to a browser via websockets: IEx in a Box :: cone.codes







