Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm wondering what's the right way to code a chat application for thousands of users.
I'm just confused how will I be able to ping the server using AJAX every second or maybe less and check if there are new records in MySQL, etc with an acceptable amount of server(s) load.
I'm currently thinking about coding this using jQuery, PHP and MySQL.
Please advice. Your help would be greatly appreciated.
Client Side
For any program that needs to poll the server I would recommend WebSockets.
I wrote an extremely basic WebSocket tutorial. I also used the web-socket-js code to implement a FlashSocket that will make it work across Firefox, IE 8+, and Chrome, as well as any browser that supports WebSockets.
I don't believe that polling would be a good choice for a chat application. While it would work, the request overhead would be much higher then using a WebSocket. The tradeoff (benefit) is that more browsers support it.
Also, hitting a MySQL database to see if there are messages is going to incur a good deal of DB overhead. I would recommend using a MySQL database for chat logs, and only keep a limited number of "back" messages on hand for new connections. Then simply broadcast new messages to all connected clients. The frontend application would then take the message and append it to chat window.
Server Side
Node.js is an evented server-side JavaScript framework. While it is still young, several very interesting applications have been coded in it. The Node.js people setup a chat program (not WebSockets) the source of which has been made available. That would be a very good place to start if not wanting to write it from scratch.
There is a PHP WebSocket implementation. Depending on your requirements it could be used just fine. Having coded in both Node.js and PHP I would say I think Node.js is a better fit for this.
On the server side, you'll need a script that can tell whether or not there is new content (eg: messages) based on a timestamp (eg: last request). On the client side, you have two options:
Polling aka Periodic Refresh:
This basically means having your client poll the server in intervals to check whether or not there is new data. What you want is to keep your requests and responses as light as possible. It could also help if run the script handling these requests in a separate process.
It will be up to you to tweak the interval to one that's acceptable for both the server and the user. You can also use a Heartbeat to tell whether or not the user is still active, so you can stop polling the server if the user left the window open but is off the computer.
HTTP Streaming aka "Comet":
Using this will require some more setup; but this is basically a long-lived connection from the client to the server and the server can "push" content to the client when necessary.
Heres a simple looking websockets example: http://www.dashdashverbose.com/2010/02/nodejs-websockets-stoopid-easy-comet.html
I assume your max amount of users would depend mostly upon your connection and server software.
You could also try out IcePush - it is an ajax framework for pushing messages FROM the server TO the javascript client. This would be a perfect match for a chatclient!
If you are a java developer, you can use jwebsocket to implement the server. there are various examples on their site to start with as I'm also going through some of them.
Briefly from their website
jWebSocket is provided to you to create innovative HTML5 based streaming and communication applications on the web. HTML5 WebSockets will replace the existing XHR approaches as well as Comet services by a new flexible and ultra high speed bidirectional TCP socket communication technology. jWebSocket is an open source Java and JavaScript implementation of the HTML5 WebSocket protocol with a huge set of extensions.
Related
I am using PHP and websocket to create a group chat application to be available on web and mobile. I've used the sample code from this tutorial: http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket
Now, I just want to get some insights if how this app can perform under thousands of connections (1k, 5k, 10k, 50k, 100k) considering that messages will be saved in a MySQL database.
I know Ratchet and NodeJS can do the job, but I want my code to stay simple as possible.
My question is, is the code available in the said tutorial is scalable enough to handle thousands of connection? If not, how can I improve it? Can I add additional websocket connections using the same code for the notifications and offline/online status checking?
Thanks
Possibly other questions similar to this one have been asked, but I think this would be something different to those that have been asked so far.
Can we create a chat app especially in PHP/MySQL/jQuery/Apache without continuous requests to server i.e. apache for a new messages if there are any? I don't want to use Comet or NodeJS, just want to know if it is possible to have a chat application that uses only AJAX to request Apache for new message using PHP.
By the way, if the answer is no, then what is the best way to have a perfect chat application in combination with PHP/MySQL/jQuery/Apache only? I think there must be some chat applications exist.
I am curious as to why you want to avoid all the technologies and methods developed specifically for these types of use cases but web sockets are the only other way to do it if you want to avoid comet.
Your chat application needs to connect to the server the read the incoming chat messages.
You have two choices:
Polling
This is continuously firing a new request off to the server at regular intervals to check for the messages. this is the typical AJAX style.
Web Sockets
Web sockets open one long running connection to the server. For a chat application if you are only interested in the newer browsers this is the way to go. As a bit of random information. Stack Overflow uses Web Sockets to check for any notifications.
I set myself a challenge today to write a jQuery chat in under half an hour. It eventually took me 40 minutes.
However, I want to improve it so the load on the server and browser isnt horrendous.
Currently the user types into a text box, presses enter, this data is sent to a .php file which updates a mySQL table and outputs all the rows on the table.
There is a set Interval on the div every two seconds to update if anyone said anything without the user pressing enter.
I just wanted to know thoughts on how to do perform this in a better way, or the most efficient way. I want to understand the best technology to use and why.
Thanks for all your input, I love stack overflow, its been invaluable to me.
Using ajax polling for a chat application with the "php back"/"javascript front" technology stack will inevitably result in a heavy server load. Http is just not designed for that kind of communication, and if you're using apache (as I assume) there is a really heavy overhead for each request.
As some of the commments indicated, you could investigate using a full stack javascript framework (i.e. Node.js on the backend).
When I had the task of accelerating an existing chat application with php backend and Javascript frontend (using periodic ajax poll), I ended up using a 3rd party server side product to handle lightweight XMPP requests. This server side product was OpenFire, but you could use eJabberd for even better performance - OpenFire is easier to set up and maintain though. The results were more than satisfactory, server load dropped significantly, and messages were delivered instantly for ~1000 online users chatting wildly away (on a less-than-average performance dedicated linux box).
It's hard to explain all the tiny details within a SO answer's scope, but luckily Ben Werdmuller # IBM went out of his way to write an awesome tutorial on this topic.
I have been in search of making live websites by using PHP. (COMET) I have been searching for a very long time already. (around a month) I have even checked some PHP chat scripts and used on my webserver, but I had some problems on most of them. (will be explained)
So far, most of the people tells the same thing: PHP isn't the best language could be used with COMET. The chat/comet examples are hacky at best.
I am asking this because, I want to have some features on my websites, like allow logged in people to chat with each other. That is where I need an alive connection to PHP pages. I am also planning on making a browser based game, and an alive connection will still be a must!
AJAX was a rare thing 2 years ago, shined with Facebook. Now pretty much everyone uses it, and it became a standard on web development. Now, the COMET based websites are growing. Youtube, Google+, Facebook, EA's Battlelog and such. I believe I should learn how to use it.
Okay, here are my questions. (Some of the information at below are the ones I gathered while searching on Google. Not sure if they're correct or not.)
Some languages like Python have special web servers designed for this job. I believe one of them is called Tornado Web Server. Developed and configured to simulate thousands of alive connections. I believe there is no such option in Appserv, but some people told NGINX can handle it with a decent configuration. Is it true? What configurations should be made? Is there any PHP web servers for this job?
Some of the most suggested things are:
a. Using APE.
b. Using Socket.IO
c. Node.js
Is there any way we can use them with PHP? Which one is the most promising? Could you give some guidance on them? Is there anything else than these?
I have used a comet chat script. Basically, you kept querying database and output the result with flush() to browser by sleeping it with usleep() in a loop. (This one became hard to understand so I will provide a quick code)
while(true)
{
// query database
// output the result
// flush the browser
// sleep for few seconds to lower cpu load
}
usleep() function basically destroyed my web server on Windows based operating systems. Is it normal to rely on usleep() on comet applications which runs on windows based OS? I mean, is there even a way to "sleep" PHP scripts? No matter what I do, CPU load goes to %100 on both WIN and UNIX servers.
Is PHP "really" that weak on this area? Should I give up with PHP and focus on other languages? If so, which language would you suggest? (That language should be promising. For example, there is no much use of AS3 after HTML5 addition, and AS3 is more likely to die soon while JS shines.)
What is WebSync? Can it be used with PHP?
Please bear in mind that I need to use COMET to make following applications:
A chat script, where logged in players will be able to chat eachother.
A browser based game. I already use JSON/AJAX and things like that when coding, but to receive opponents steps, I should pull the data, so an alive connection is needed. (Don't imagine advanced games, I am speaking about games like chess at best.)
I would appreciate if you can give me a short guidance. After all, I have been getting confused day by day since there are SO MANY random articles on internet. People even call setInterval() as COMET, so it is a complete mess.
There needs to be some clarification here. What you're looking for is not language specific per se. If you wanted to achieve the comet functionality with PHP you'd be relying on the Web Server (e.g Apache) to do the HTTP streaming. Also you need to understand that this is not supported in HTTP versions prior to HTTP/1.1. With that said, if you want to have a non-blocking event based web server, you're better off looking at Tornado and Node as suggested.
Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it This technique will produce more responsive applications than classic AJAX The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.
but it's create lots of traffic on your web server. If you want to build chat application in PHP use pusher which is a third party service and easy to use.
here is a link for pusher https://pusher.com/tutorials/realtime_chat_widget
the second suggestion is use ratchet for creating a chat application.
here is link for ratchet http://socketo.me/docs/hello-world
i hope it will help you
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been thinking about creating a chat application, but don't know too much about how to create an effective one so I was hoping we could talk about the "best" and most efficient(performance) way to making one.
So the base features are probably seeing each other's messages and a chat log.
So I was considering using codeigniter and ajax to create the view and sending of each message and every time a message is sent, the message is sort in a table for the chat log. Then I thought, if every time a message is sent a query is sent, the scaling of this app would be horrible, so I was thinking of storing the log on the client's cookie or javascript object and upon leaving the chat, the log is sent and stored in the database. But then I don't know if this would work or would be extremely bad security wise.
Also, I was reading stuff on stackoverflow about chats and websockets and ran in ajax APE as an effective way to manage sending messages... But I don't really know much about websockets and how to use them etc...
Anyhow, I'm pretty sure I can make a chat application, but a good, effective, scalable one, no...
Any suggestions on the best/most effective way on making a chat application?
Some of u suggested node.js or socket.io.
Since I don't know anything about those two, which one is better?
Also I noticed that in the node.js example that he uses one server for the whole chatroom, does that mean I need a node.js server for every chat/chatroom?
Even though you are thinking about building this with PHP and AJAX, I highly, highly recommend doing something like this with Node.js if that's an option for you. It is much more suited for something like this; especially compared to AJAX which uses expensive HTTP requests in relation to Node.js A big benefit of it is that it is incredibly fast at doing many kinds of I/O and is asynchronous. Also, the Node.js process is non-blocking which helps make it even faster.
You should have a look at this example as well. The code was written by the creator of Node.js himself. Also, Node.js can interface with databases so you don't need to use text-based logs or anything.
If you're interested in WebSockets but don't want to deal with catching all of the cross browser quirks, definitely check out Socket.IO. It's a library for working with WebSockets and I've found it to be very good at dealing with all of the different browser versions out there.
Hope that helps!
node.js and socket.io seem the most appropriate for this task.
node.js allows to handle many persistent connections with low memory usage, which is quite suitable for handling all chat clients. socket.io is an abstraction over websockets / comet / long-polling that allows to pass messages between the node server and a web browser.
You can still use codeigniter or any other framework for the user interface.
If you were really going hard core big, you might want to think about replacing the database for message queues. Second Life put up their message queue evaluations for in-game messaging.
http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes
Although, chat logs would not likely be persisted on your server like they would with a database.