A little help with phoenix framework

Hello, I’m trying to get the socket by the data I already assigned to it

for example, I’ve set the user-id and nick name

socket = assign(socket, :user_id, USER_ID)
socket = assign(socket, :nick_name, NICK_NAME)

And I have User A with ID 1 and User B with ID 2, And I have a function called get status with a parameter of USER_ID

User A will call that function and pass the ID of User B which is 2

Now I want to get the nickname using the USER_ID

can anyone provide me an example?

I will appreciate it a lot!

I think you should pull that data from somewhere else (for example database), not the socket.

Hi, But i have 1 million CCU, reading and writing everytime i call this function would be an overkill !

assign/3 is just for an association not for storing. So if you want to make a get probably you should use a GenServer to track the user_id => nickname pairs and monitor socket to remove this pair once socket is disconnected

I used gen server and ets table, when user join i insert to the table and when i want to get the data i call genserver.call, what do you think?

I think that for the start it should be more than enough. I never did a benchmark for the case like this, not sure that it would resist against 1kk read at the single moment :slight_smile: But if you feel that it is a bottleneck then you always have an option to access :ets from another process(your channel) to read and write, then don’t forget to setup :ets with public access.

1 Like