D
D
Dmitry Tarasov2020-01-17 21:46:38
JavaScript
Dmitry Tarasov, 2020-01-17 21:46:38

How to implement notifications and messages like in VK?

Hello!
I decided to make real-time notifications on the site and update messages in the dialog.
The site itself is in php, the nodejs server is also running.
In node: express, socket.io, redis, etc.
How was it for me?
Let's say how notifications were implemented?
It's just stupid ajax requests n number of times per minute, because of the large online and users there could be a bunch of tabs, plus they go through the tabs, in general, the database almost constantly received too many connections.
I decided to remake everything on a node with a websocket connection.
I identify the user, create a unique room for him based on his id, and I also put all the user connections in a separate array.
If the broker received some kind of notification, I send all these notifications to the room, for a specific user, if there are a bunch of tabs, then there will be a notification for all open tabs, I want to do it like in VK, if let's say the dialog is open and the window is active, no notifications, we just show a new message, if the dialog is open, but the window is not active, then the notification sound, if the dialog is open, but another page of the same VK is active, then the notification will be on the active tab, how to achieve this?
The problem is that the notification goes to all open tabs and you need to understand if the dialog is open, which tab the person is currently on, etc.
How to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
marataziat, 2020-01-18
@marataziat

To understand if the user is in a tab: https://stackoverflow.com/a/20922020/7415288
There is a feature in browsers so that the tabs of the same site speak to each other: https://developer.mozilla.org/en-US/ docs/Web/API/B...

R
Roman Mirilaczvili, 2020-01-17
@2ord

almost constantly the database received too many connections

Perhaps the time between opening a connection and closing it takes a long time and therefore the DBMS is forced to keep many open connections at the same time, which is bad.
Most likely, the code needs to be corrected.

V
Vlad, 2020-01-23
@p1rat495

I don’t know how it is now, but earlier it was implemented through iframe and postMesaage().
look here
PS One connection is opened only on the first entered user tab, then all notifications are sent to the rest of the tabs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question