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.

First 10 of 16 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement