Need some guidance on working with storing volatile user data on Pheonix sockets

I’m going to keep it short!

I’m wondering the best way to store constantly changing user data with their websocket connection in a Phoenix app (Phoenix.Socket). Do I just put it in the socket.assigns?

The problem is, I’m trying to simulate location based messaging rooms, where only users within a certain radius of your location receives your message. Is this possible with Phoenix channels? Dynamic rooms? I want an efficient way to query users within a certain location. For this purpose I was thinking PostGIS would be too latent due to how short lived a users connection would be. Would some efficient data structure be best to store locations? Something like a k-d tree?

Thanks in advance!

For socket-specific stuff, yep. :slight_smile:

Hmm, I’d probably use phoenix presence with the key being a quantized location, then listening for each quantized location within this sockets range, have the presence data have the precise location that way simple test of distance within this quantized space can be done for accuracy on individual messages.

PostGIS wouldn’t be bad, especially when starting to scale up plus combined with postgres pubsub as well.

If you want another process to handle it then there are tons of structures to keep it simple.

For note, abstract it out into it’s own module and keep it simple (like phoenix presence quantized areas or so) and you can always change it later if the need arises. :slight_smile:

1 Like


So do you think channels are necessary at all? Should I make my own implementation of Phoenix.Socket.Transport? Then use Phoenix.Presence to watch that user socket? I was looking at presence earlier, saw some promising things there. Location data seems categorically similar to presence (online,offline, typing, etc.).

Just to be clear; I’d user either PostGIS, or Phoenix.Precense? Both?

If you need to push data back tot he client, use channels. :slight_smile:

Even while using PostGIS for ‘area’ checks you’d still want presence for ease of knowing who’s online or not. :slight_smile:

1 Like

Oh, and don’t forget that a single socket/channel can connect to ‘many’ Presence topics!