Phoenix websocket 1:1 messaging

hello, sorry me, I’m a newbie

studying websocket phoenix channels, pubsub, transport

easy for broadcasting, but for 1:1 websocket messaging?

In C or in Pascal I will use an hash table, or dictionary, to associate the handle of the socket to a username, then when someone send to another simply will lookup the username to retrieve the socket then sending

In elixir/phoenix what is the most direct mode to achieve this one to one communication? (as example associating pid of the process/socket to a username?)

thank yuo

1 Like

please

also I want to access at lower level to the websocket json packet, both read and write, eg. to put a json value before send, or to retrieve all the passed headers/infos into the socket/pid/descriptor map/struct

1 Like

Implement Phoenix.Socket.id in your MyAppWeb.UserSocket module: https://hexdocs.pm/phoenix/1.5.3/Phoenix.Socket.html#c:id/1

You can look at serializers for this one:
https://hexdocs.pm/phoenix/1.5.3/Phoenix.Socket.Serializer.html
https://hexdocs.pm/phoenix/1.5.3/Phoenix.Socket.html#module-client-server-communication

But keep in mind that phoenix channels are an abstraction on top of multiple transports and not just websockets. If you want low level websocket handling you’re likely better of integrating directly with cowboy.

1 Like

You still want to use phoenix channels and pubsub. If I’m messaging you, and open two tabs, that’s no longer 1:1. There are two subscribed channels for me, you need pubsub.

3 Likes

in this case I will build a mini parser into js to use one only tcp channel and many windows (eg. label, msg), then on server parse from, label, msg?