0

I'm currently developing a website for a chat application (everything in the serverside is Django).

One of the problems I faced was how to keep the website (once a user is logged and has everything rendered) updated if the user needs to receive something new (a new message, notifications, etc).

The solution I came with was to first create a URL to send a get request, and the response would be a list of unseen notifications for the user. Then, in the HTML, a JavaScript code to send request to this URL and receive the unseen notifications.

This way, upon loading, the page will send a get and receive all unseen notifications and save them in a variable, after that it will keep sending the request every half a second and check if the rendered data is the most recent data, and if it's not, reload the page to refresh all the rendered data.

Now, this works ok, but I'm not sure this is how it should be done, as I'm bombarding with requests my server (currently, everything in development and just a couple of users at the time nothing exploded) and the client is sending request all the time. Is this how other webs (for example Facebook) keep themselves updated in case a new notification appears without the need of the user manually reloading the page?

Thanks in advance!

  • 1
    You should not be using Ajax requests to pull data from the server. Since yours is a chat application , using sockets would be better. Django has channels for real time communication. – Gary Mendonca Jul 03 '18 at 15:19
  • Please read https://stackoverflow.com/questions/34730334/how-websockets-are-implemented and https://stackoverflow.com/questions/33687298/how-to-send-push-notification-to-web-browser – Luis felipe De jesus Munoz Jul 03 '18 at 15:21
  • Look into: [`Firebase`](https://firebase.google.com/), [`Socket.io`](https://socket.io/) and [`Web Sockets`](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications) – Alex Jul 03 '18 at 15:22
  • What you can use are AJAX, Sockets, Django channels etc.I developed a complete chat application using AJAX. Hope it helps! – Pujan Mehta Jul 04 '18 at 11:21

0 Answers0