Phoenix channel numbers

Hello guys, I’m trying to connect to Phoenix Channel via python client to receive messages.
I had no luck in receiving messages, so I went into the site to take a look at their WS messages. What I’ve found was that they have "4","4" in front and it seems like it’s channel related, but have no clue. This is a public endpoint btw.

enter image description here

Are these numbers important to be included in the request or something?
For those of you who might be interested in my code, it looks like

import websockets
import asyncio

 async def test():
    async with websockets.connect('wss://') as websocket:
        data = dict(topic="updates:CAD-BTC", event="phx_join", payload={}, ref=None)
        await websocket.send(json.dumps(data))
        while True:
            msg = await websocket.recv()


Yes, Phoenix Channels aren‘t plain websockets. In fact websockets are only one available transport protocol.

The other part is the serialization protocol used. Phoenix ships two versions. The older one used json objects, while V2 uses a json array to save a bunch of bytes for not needing keys.

So connections need to negotiate the serialization protocol and then communicate based on messages serialized to it‘s definition.

Then there are the abstract concepts of channels, which are independant from the low level implementation like joining channels, waiting for responses, phoenix presence, …

1 Like

These numbers are opaque references provided by the client to the server.

Once you’ve opened a WebSocket, all messages sent and received in that connection are tunneled through just one socket. In particular, replies to messages. The WebSocket protocol does not have support for topics or anything, those are abstractions provided by Phoenix on top of raw WebSocket payloads. WebSocket does not have the concept of “replying” to a message, and if you are subscribed to several topics messages go back and forth intermixed.

Those opaque references are echoed back by the server in its responses, so that the client is able to match to what message is the reply replying to.

1 Like

I see… so I need to somehow deal with serialization protocol and also solve parsing V2 json array…
Found a relevant python library, but this one is configured to V1. (doesn’t work I tried)

Library looks not that complicated tho. Any tips how to adjust it accordingly?

If you want to see how’s a client implement, the official one for JavaScript can be a model.

1 Like