quda

quda

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 :thinking:

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 :rage:
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.

Showing Posts 1 to 10

cmo

cmo

What about ws://localhost:4000/socket/websocket?

quda

quda OP

Nope, the same.
Why do I have the impression that these “channels” work only with the Phoenix client?!?

LostKobrakai

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.

quda

quda OP

As suspected… proprietary implementation. :face_with_raised_eyebrow:
The URL format might be embedded in this “protocol”.
Which is this non-standard message protocol pls ? There can I find the doc ? Tx.

antoine-duchenet

antoine-duchenet

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).

LostKobrakai

LostKobrakai

I’m not sure where you got the expectation from that channels are plain websockets.

Here are the docs on channels (Channels — Phoenix v1.8.8) 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.

The transport is driven by Phoenix.Socket.Transport — Phoenix v1.8.8 and the message format by Phoenix.Socket.Serializer — Phoenix v1.8.8.

If you want to use the existing serializers then you can find them here: phoenix/lib/phoenix/socket/serializers at main · phoenixframework/phoenix · GitHub
The format is negociated on connection.

quda

quda OP

You’re right, my wrong. :neutral_face:
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

LostKobrakai

LostKobrakai

I mean you could always integrate directly with cowboy. That’s essentially what phoenix does with it’s sockets.

LostKobrakai

LostKobrakai

@quda here you can find an example, which proxies iex to a browser via websockets: IEx in a Box :: cone.codes

quda

quda OP

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.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews