V
V
vanilich2015-09-23 21:31:54
PHP
vanilich, 2015-09-23 21:31:54

Comet (php + js + mysql). How to implement instant messages?

Good day.
I'm developing a web application and ran into a small problem. Namely, with the implementation of the instant messaging service. I read about Comet on the Internet, the idea came up to raise a separate server for messages.
Everything seems to be clear, and it’s not a problem to implement, because. You can find many bicycles on the Internet.
But I have a question. How will the server check for new (unread) messages? Does it really have to access the database every 5 seconds (in this case, mysql) and pick up new messages, and then give them to the user. It turns out a delay and a rather big load on the database. How to be? What can you advise? How can the server track the appearance of a new message in real time and immediately give it to the client?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vanilich, 2015-09-27
@vanilich

Thanks everyone for the replies. I read about node.js and socket.io. Interested. Downloaded, installed, played - liked it. I think for my task just right.
Speaking closer to the implementation, I will do this:
1. When a client connects to the server, I will check its session and take its id from the table. Id and socket.id will be stored in an array of the form:
2. I will create a handler, ala "onClientSendMessage", where I will check to whom the message is sent.
3. If the person who owns the sent message is currently connected to the node.js server, then I will take his socket.id from the clients array by the "id" key, and send him a message accordingly.
4. If the person is not connected to the server, then I simply save the message in the database.
5. If the client terminated the connection, then I will remove it from the clients array.
It seems so ... Thank you all.

L
Loot, 2015-09-24
@Cesavel

I advise you to look towards web sockets

S
Sergey, 2015-09-23
@goodwin74

I think the Nginx module - nginx_push_stream - can be great.
Here is an article on Habré: habrahabr.ru/company/cackle/blog/167895

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question