Using phoenix channels without a single-page-application?

I’m new to all this so apologies if it’s a dumb questions. I would like to add server-push messaging from genservers running for each active user to the users js code. I can see that Phoenix channels will do this. My concern is that the client code is not a single page application, it’s an older project with mostly new page loads every time a user moves somewhere. I was thinking a js script would reconnect on each page load to the channel. What I’m wondering is whether this will work for having messages heading for a particular user get queued up? Or do I need to worry that if the user is disconnected at the moment a message goes out, they might not get it? Any tips appreciated.

2 Likes

Looks like you need to implement your own server side buffer:

https://hexdocs.pm/phoenix/channels.html#resending-server-messages

Phoenix uses an at-most-once strategy when sending messages to clients. If the client is offline and misses the message, Phoenix won’t resend it. Phoenix doesn’t persist messages on the server. If the server restarts, unsent messages will be gone. If our application needs stronger guarantees around message delivery, we’ll need to write that code ourselves. Common approaches involve persisting messages on the server and having clients request missing messages. For an example, see Chris McCord’s Phoenix training: client code and server code.


3 Likes

Even if this was a SPA, you need to have a plan for dropped messages, since users can end up disconnected from your server because they’re switching wifi network or their tab crashed.

3 Likes

Thanks for the input and tips y’all. :slight_smile:

2 Likes