Update sockets of channel without pushing to client

Hi to all, as I said in a previous question, English is not my native language so… sorry for the mistakes.
I am developing a game with Phoenix channels, each room (channel) of game has two players and I am keeping the score of each one using assigns in socket.
My question is, in order to update the score of the correct user, is the “phoenix way” broadcasting a fake message, intercepting it with the “handle_out” function, decide if it is the correct user to add 1 point to the score, but not pushing any message to the js client.
I hope the question is clear.

Many thanks

1 Like

Could you make a channel for each user, eg: “game:{user_id}”

2 Likes

Thanks tallakt, I thought that but I need the two players.
I was reading an old thread here, where @chrismccord told to use Presence… maybe that’ s the proper way.

Thanks again, this is a nice community.

1 Like

I see some alternatives that do not include handle_out, even if handle_out is a fair implementation imho:

  • One channel for a game with two assigns, one for each player’s score. Both players receive the same message, but ignore the ‘other’ score in the javascript code

  • One channel for each player. The channel has an assign for score, and messages are sent only to a single player

  • A combination: Three channels, one for each player and one for the game. At startup, javascript will connect to two of these channels depending on which player you are representing. Common game data is sent on the game channel, while player private data such as score is sent on the player channel

In any event, I don’t think the warning in the Phoenix docs apply to using handle_out, as you would only be broadcasting to two users at a time, so hte overhead of processing these individually would probably not be severe.

Phoenix presence I believe would be more useful for keeping track of which player is online, and also for tracking the scores (instead of using assigns to the sockets). This is a bit of guesswork for me until I get a chance to study this at a later time though.

3 Likes

In this iteration of the code I will keep the strategy of two sockets per channel, but I think that the three-channel way is clean, the only thing I need to know is how I communicate between different channels using the public API.

Thanks again.