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
Related
I'm creating a mobile app with a server backend that will authenticate a user and continuously send them updates whilst listening for post data from the mobile app. These updates will be specific to the person, pulled from a database.
From my research it seems that I should use a websocket. I'm familiar with PHP so have tried Ratchet. I've created a simple chat script with Ratchet which queries a database onMessage and sends the data to the client.
My question is, are websockets right for this? When a server receives a connection it must query the db every 5 seconds and send updated info to the app. It must listen for messages that will change the db query. Everything in Ratchet's docs seems to be focussed on subscriptions to topics rather than treating each client individually, although I've gotten around this by using:
$client = $this->clients[$from->resourceId];
$client->send("whatever_message"):
Am I complicating things by using Ratchet? Or should I use a child process to handle each client?
I am sorry for a vague questions. I've researched as best I can but cannot establish whether I'm heading in the wrong direction! Thank you for any help.
That is a good formula. Sending post data from the apps while maintaining a socket connection is a good distribution of processes. However PHP might not be your best option for running the socket server.
The reason for this is PHP is a single threaded language which doesn't sport an elegant event system.
Take NodeJs as an alternative. It too is single threaded, however you can register events on socket servers allowing the software to run additional control processes while it waits for network activity.
This does not limit you to javascript however. Work can still be delegated to PHP processes from the NodeJs application (I use NodeJs as an example only, there are other options such as Java, Python, or good ol' native).
For moving work to PHP, you can either execute commands, or use a job server to enable synchronous and asynchronous tasks.
Here are a few resources you can combine to accomplish this:
http://nodejs.org/
http://socket.io/
http://gearman.org/
http://php.net/manual/en/book.gearman.php
And if you are using Symfony:
https://github.com/mmoreram/GearmanBundle
I have this "crazy" project starting, the idea behind it is quite clear:
There is some software that writes to MySQL database. The interval between queries are 1 second.
Now I need and web interface which loads those database records, and continues to show new records, when they happen.
The technologies I am supposed to use are PHP and HTML5 WebSockets. I'v found this nice library Ratchet which I think fits my needs, however there's one problem, I am not sure how to notify PHP script, or send a message to running PHP WebSockets server when the MySQL query occurs.
Now I could use Comet and send request for database record every second, but then it beats the WebSokets which I am supposed to use.
So what I really need is MySQL Pub/Sub system.
I'v read of MySQL triggers but I see that it possess some security risks, and thought the security in this case isn't a real concern since the system will be isolated in a VPN and only few specific people will be using it, I still would like to address every possible issue and do everything in a right way.
Then there is MySQL proxy, of which I have no knowledge, but if it could help me achieve my goal, I would very much consider using it.
So in short the question is how can I notify or run PHP script when MySQL query occurs?
I would separate the issues a bit.
You definitely need some sort of pub/sub system. It's my understanding that Redis can act as one, but I've never used it for this and can't make a specific recommendation as to what system to use. In any case, I wouldn't attach it directly to your database. There are certainly database operations you need to do on your database for maintenance purposes and you don't want it flushing out a million rows to your clients because it's based on a trigger. Pick your pub/sub system independent of what you're doing client-side and what you're doing with your database. The deciding factors should be how it interacts with your server-side languages (PHP in this case).
Now that your pub/sub is out of the way, I would build an API server or ingest system of sorts that takes data in from wherever these transactions are coming from. It will handle these and publish messages as well as inserting them into the database at the same time.
Next, you need a way to get that to clients. Web Sockets are a good choice, as you have discovered. You can do this in PHP or anything really. I prefer Node.js with Socket.IO which provides a nice fallback to long-polling JSON (among others) for clients that don't suppot Web Sockets. Whatever you choose here needs to listen for messages on your pub/sub and send the right data for the client (likely stripping out some of the information that was published that isn't immediately needed client-side).
I am designing an app that will have to periodically check a web address for updates.
The updates will be in a MySQL database tables.
I know the idea way to do this, is to create a service that is constantly running and trying to check for an update periodically (lets say 10 seconds).
Below are some questions that are unclear to me as I start my quest to accomplish this task.
Do I need to manually do the check for an update everytime from the client to server ? (that is, take a value on the client side, send it to server side, and do a head to head comparison), OR, the php/MySQL server can send a notification that there is an update that took place.
I came across a RSS feature in several posts in SO, however those tend to be NEWS applications, mine is not anything like that. Does it help to use RSS feeds in here ?
I'm planning on doing the check every 10 seconds. This means that upon the install of the app and since the app is on the device (and as long as there is internet connectivity) I will keep on fetching the server. This tends to be bandwidth and ram/cpu consuming for the client side. How do big apps like Viber, WhatsApp manage to do so ?
Just thinking out loud - , in order to avoid such a hassle, I was thinking with each update on the server, send a notification to the user with a certain code, and do the math inside the onReceive, if the code was something related to an update, 1-not show the notification to the user, 2-run the server update check thing.
ideas ?
The trouble is traditional php/html systems are client centric (the client has to initiate all communications).
You might want to look into websockets, or node.js. These technologies will allow you to create a persistent connection between the server and its clients. This in turn will allow you to 'push' data to the client when appropriate, without the client having to ask for it every x amount of minutes.
Node.js Chat Example
Node.js info
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.
I am interested in a really good way of doing instant messaging like meebo and facebook and myspace all have, also for notifications on a page. Example on my site now, when a user receives a new mail message, photo comment, profile comment, friend request, some other things, they will receive a notification message and link on the page they are on using jQuery and AJAX.
I believe on a large scale that this is not the best way to do it though as my jquery code will have to basically run a PHP script to query the mysql table every 30 seconds or so for every user who has the page open with this script running. I would like to eventually add in some sort of instant messaging like facebook has in a chat bar at the bottom of the screen if I can find out an efficient way of doing it.
I have heard the terms Comet and HTML5 WebSockets but I am not sure if these are the solutions I should be looking at for such a feature? I would like to keep bandwidth at a minimum and running a query every few seconds does not seem very efficient on a high traffic site.
If you know some good solutions, please explain how they work a little bit please
You're looking for a "comet" server. Since you're talking PHP, your best solution would be a SaaS solution, such as WebSync On-Demand, since PHP has issues with scaling when it comes to large numbers of concurrent connections being held open. See also this question.
I think Facebook uses XMPP for their chat, so have a look at it.
XMPP is
an open technology for real-time communication, which powers a wide range of applications including instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.
You'd install a separate server to handle messaging and in your app you'd implement a client with one of the available libraries.
Could you have a shared message queue that ajax can query? Maybe its a php script that stores a majority of the message in memcached (and possibly write to db in case of failure).
Not sure, but curious to hear other solutions ...
The basic problem is that you need to implement a Comet server (ie implement Server Push). The standard web hosts and apache don't readily allow you to do this easily. Another hosted solution (apart from Web Sync) is Goldfish Server. Currently free.