Answer the question
In order to leave comments, you need to log in
How is the notification system a la Vkontakte organized?
How is the notification system, such as a contact, organized?
The list of events (new message, new friend, new gift) are stored in a separate database and retrieved by Ajax every n-seconds or in another way?
Who has any thoughts, and how best to organize?
Answer the question
In order to leave comments, you need to log in
More like a Publish-subscribe
pattern . The front end subscribes to some type of message on some channel (for example, new-message
, new-friendship-request
, new-gift
on channel user-ID
) and performs some action on the data of these messages (updates the unread message counter, makes a new notification via the Web Notifications API, whatever) . And the server, in addition to writing data to the database, sends a new message of some type to a specific channel.
Those. someone sent a new message. The server writes it to the database and pushes it to the client:
message = Message.create params[:message]
Pusher["user-#{message.receiver.id}"].trigger('new-message', message)
var channel = pusher.subscribe('user-500fbf726446c604d2000001');
channel.bind('new-message', function(data) {
alert('Received new message from ' + data.sender.name + ': ' + data.text)
});
You have a browser. We go to vk.com and see that, for example, in Opera, long-polling requests are sent to qNN.queue.vk.com/im305, where the user id, tricky key, timestamps are sent, in response a code like ([{ "ts":"1567120607","events":[]}
Obviously, they have a network of servers on which demons hang, I don't know what technology, maybe C, maybe Node.JS, maybe something else that , on the one hand, they accept requests from clients, on the other hand, they receive notifications.I
think the easiest way in the code is for some event (for example, sending a message / adding to friends), a notification is sent in parallel to this daemon, and it relays this update to those interested users
If I needed such a daemon, I would use the D language and the libev library for development.
But you can try to write such a daemon in PHP, I think it will definitely withstand 10-20 simultaneous users. Or you can take a ready-made daemon - PHP Multiplexor or something like that, from DKLab.
I saw a cross-browser solution on Habré - polling a file given by a light web server with reading the modified header. If changed, read. And why keep a lot of persistent connections?
Apparently, there is just a subscription to events. I looked through the developer tools - every few seconds a request is sent with a list of id and a waiting time (as I understand it, the higher my activity, the less the waiting time):
ts:1233434603_12354100_1238569453
wait:25
I get back:
[{"ts":"1233434603","events":[]},{"ts":"13254101","events":[]},{"ts":"1238569454","events":[]}]
perhaps the observer pattern will help you, try looking in this direction.
It is better to use longpooling in cases where you need to immediately show the user a notification, well, or every N seconds to pull the server. You make a request to the server “give me new friends” and if there are none, keep the connection open, and close it either by timeout or when a friend appears. Repeat.
I can’t check how the contact works, since I’m not at the computer, but most likely long-pulling is used there
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question