Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make fast, lightweight, economic online chat with PHP + JS + (MySQL?) + (AJAX?)

What way will be best to write online chat with js? If i would use AJAX and update information about users and messages every 5sec - HTTP requests and answers will make big traffic and requests will make high server load.

But how another? Sockets? But how..

like image 682
user532801 Avatar asked Dec 06 '10 20:12

user532801


2 Answers

You seem to have a problem with the server load, so I'll compare the relevant technologies.

Ajax polling: This is the most straightforward. You do setTimeout loop every 5 seconds or so often to check for new chat messages or you set an iframe to reload. When you post a message, you also return new messages, and things shouldn't get out of order. The biggest drawback with this method is that you're unlikely to poll with a frequency corresponding to how often messages are posted. Either you'll poll too quickly, and you'll make a lot of extra requests, or you'll poll too slowly and you'll get chunks of messages at a time instead of getting them in a real-time-ish way. This is by far the easiest method though.

HTTP Push This is the idea that the server should tell the client when there are new messages, rather than the client continually bothering the server asking if there are any new ones yet. Imagine the parent driving and kid asking "are we there yet?", you can just have the parent tell the kid when they get there.

There are a couple ways to both fake this and do it for real. WebSockets, which you mentioned, are actually creating a stream between the client and the server and sending data in real time. This is awesome, and for the 4 out of 10 users that have a browser that can do it, they'll be pretty psyched. Everyone else will have a broken page. Sorry. Maybe in a couple years.

You can also fake push tech with things like long-polling. The idea is that you ask the server if there are any new messages, and the server doesn't answer until a new message has appeared or some preset limit (30 seconds or so) has been reached. This keeps the number of requests to a minimum, while using known web technologies, so most browsers will work with it. You'll have a high connection concurrency, but they really aren't doing anything, so it should have too high a server cost.

I've used all of these before, but ended up going with long polling myself. You can find out more about how to actually do it here: How do I implement basic "Long Polling"?

like image 72
Brent Avatar answered Sep 19 '22 17:09

Brent


You should chose sockets rather than AJAX Polling, However there isn't much around about how you can integrate socket based chats with MySQL.

I have done a few tests and have a basic example working here: https://github.com/andrefigueira/PHP-MySQL-Sockets-Chat

It makes use of Ratchet (http://socketo.me/) for the creation of the chat server in PHP.

And you can send chat messages to the DB by sending the server JSON with the information of who is chatting, (if of course you have user sessions)

like image 29
André Figueira Avatar answered Sep 19 '22 17:09

André Figueira