Phoenix Socket - Send/receive message from an individual socket, not across a channel or by user ID.

Hi, I’m building a realtime reflex game in Phoenix. Because fast response times are important, I want to display live ping values for the connection. My plan is to have the server send a ping event to each socket, wait on a pong reply, calculate the timestamp diff, and then send down the ping ms along with the next ping event, repeating.

However, I am not sure how to implement this. I don’t think I can create a "ping" channel because I’m not sure how to disambiguate “pong” replies per-socket. I am also unsure about any overhead that channel broadcast may add to the ping value versus sending to the socket directly - possible it’s a nonissue.

My game does not need authentication, so suggestions to create a per-user-id channel won’t work because sockets are anonymous. Even if authentication is suggested as a better practice, I also want to display ping per-connection, not per-user - that is, if the user is logged in on a phone and on desktop, I would want each connection’s independent ping value displayed.

I came across a few other topics on the forum:

However, all suggestions involve a user_id of some sort, which, as I think I understand it, would still cause ping events to be sent to all sockets under that user ID, not sent per-socket.

Any advice appreciated here. Thanks in advance!

Hello and welcome,

You can assign a uuid to identify each socket.

I often use this package to do so… https://hex.pm/packages/uuid

1 Like

Oh, that is such a simple solution and makes a ton of sense. Thanks!