Answer the question
In order to leave comments, you need to log in
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
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question