Question

I am trying to use PubNub to set up a system for pushing simple real time messages from my web server to clients. I am running Django on Heroku.

In particular, I'm interested how to establish a channel between the server and the client, which this requires sharing a unique channel id between the two.

Our website is a type of social networking site -- lots of users will be logged in at once. We just want a way to update their interface when they get something like a new message or friend request, without requiring a refresh.

I have an idea of what to do, but I thought there might be some best practices I'm not aware of, so I wanted to get some feedback.

  1. At login, the client posts to an API endpoint
  2. The handler function for this POST checks to see if there is already a channel id record in the database for this user.
  3. If so, it updates the record with a new channel id (which is a UUID). If not, it creates a new record with this user's id and a channel id.
  4. Return the channel id to the user.
  5. On logout, this record is deleted.

Technically, I am worried this violates REST principles. The single call to the API end point functions both to create data server side, like a POST, and return data to the client, like a GET.

I'd appreciate any thoughts on relevant best practices. Thanks!

Was it helpful?

Solution

It sounds like you'd benefit from using Pusher. They are more or less the defacto hosted websockets provider that lets you have clients subscribe (through their browser with sockets) to Pusher, listening on various channels. You then send simple API calls to Pusher, which broadcasts it to all the connected clients.

This is the best way to handle realtime updates to many clients logged in at once, without doing polling via AJAX, or rolling your own websockets stuff.

I highly recommend giving pusher a look. (As a note, their free tier is extremely generous.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top