Can I add assigns to the "main socket" after the connection is already established?

As explained by this post, when the client first connects with the server, a “main socket/process” gets created and holds its assigns. Later, when the client joins specific channels/topics, each channel’s socket/process copies those assigns and can add to them as it will.

I now have a use case where, upon the user joining their own individual channel (i.e. user:#{user_id}), I retrieve some information about the user from the DB, which should then be globally available to all channels this user later joins. However I haven’t been able to find a way to put those information into socket.assigns so that they can be available everywhere. If I try to assign them, they will only be available in the socket.assigns of this particular user:#{user_id} channel.

Is there a way to do it? Should I just instead simply try to fetch all those information in one go when the user first connects, instead of when they join the individual user channel?

1 Like

If all the channels need it, you should load it in the socket and set it on assigns. If that’s not possible you can publish a message to the other channels and have them update their assigns.


This would work. An answer on SO by Aleksei Matiushkin suggested maintaining permanent states in user_id => user_info pairs using other solutions such as Agent , ETS , DETS , mnesia, which would be a solution as well.