Cowboy will spawn process for each request (in this case it is websocket connection, event source request or long polling through ajax).
Sending messages between users are resolved simply by sending message to appropriate process representing the connection to that user (if you have two users and both have websockets support, than there are 2 processes, each representing websocket connection).
Lets say i have 2 users (Foo, Bar).
Foo wants to send message to Bar, so the Foo process must somehow to obtain pid that is associated with cowboy process representing connection to user Bar (so he can send regular erlang message to him - the message is internally sended to user webbrowser).
But what happened when Foo obtained the pid and the pid will become invalid, because the user Bar will reconnect in the meantime (reconnect means that the process associated with Bar was terminated and because Bar connects again, he gets new process)?
Bar reconnected because of network problems.
That means if Foo will send message to Bar (Foo has pid of process, that was terminated already), the message will never be delivered.
The first solution is that because the message should be probably persistent (like messages on facebook for instance), you will always save it to DB before you send it to the user. When the user connects, there must be done some synchronization, because even if he was 99% of time online, there could be massages he doesn`t have. So Bar will gets message from Foo when he sync after reconnect (message will be pulled from DB).
NOW THE REAL PROBLEM IS: what to do if Foo will have that invalid PID too long, that he will send some messages even after Bar reconnects and performed sync? The message for Bar will be stored in database, but because it was stored after Bar did sync, it will not be delivered.
#msg{from,to,msg,timestamp}
. When user reconnects, it ensures that he/she first receives all old messages in correct order before an new msg is sent to them. – Muzaaya Joshua