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
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.
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.
As suspected… proprietary implementation.
The URL format might be embedded in this “protocol”.
Which is this non-standard message protocol pls ? There can I find the doc ? Tx.
I just tried to connect to ws://localhost:4000/live/websocket on a brand new Phoenix project and it worked (live is the default LiveView socket path, it could have been any other socket path).
As @LostKobrakai said, Channels is not “arbitrary websocket messaging”, it’s mostly a multiplexing / pubsub protocol, built on top of websockets and able to fallback to longpolling. I suppose it could be considered similar to the Socket.IO implementation (you can read more on the socket.io-protocol, not to be confused with the engine.io-protocol, here if interested).
I’m not sure where you got the expectation from that channels are plain websockets.
Here are the docs on channels (Channels — Phoenix v1.6.6) and they very plainly state that channels are transport agnostic, with websockets and longpolling being supported out of the box and some additional 3rd party clients.
You’re right, my wrong.
I need a standard, open websocket implementation to build my API on top of it. I wanted to do it in Elixir, unfortunately I have to change framework/language
Thank you @LostKobrakai, nice info set you provided.
I will take a look into your example + cowboy websocket implementation but I have the feeling that would be easier and faster for me to drop Elixir for this task and to build it using js/Node, tools that are way more familiar for me.
@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:
I’ve successfully built a 3rd party API on top of Phoenix channels. There are a few implementations in other languages that you can see here, Channels — Phoenix v1.6.6
In addition I built a Javascript library that wraps the JS Phoenix channels client to provide a more succinct API to our backend, frontline-client - npm
@entone & @TwistingTwists thanks both but it was not what I was looking for: an agnostic implementation of ws with Phoenix channels. The channels architecture does not allow that, so I have to go at a lower level and build up from there: Plug cowboy → cowboy_websocket. I’ll tackle this another time though.
For the time being I already built a dirty quick solution for my needs on Node.