John Davidson

php - SSE or Websockets or AJAX - Server Load


I'm a beginner/intermediate programmer and this is my first post.

I've started a business that is booming at this point.
I pretty much learned PHP, Javascript and MYSQL while building this business and now, a year later, I'm at the point of redoing it all because of messy / poor code.

The problem I have (if it even is a problem) is the amount of AJAX requests we send, and the potential load its putting on the server.
I've got 80 tablets and 20-40 phones sending AJAX request every 30-60 seconds, that's not including our actual customers that are using out site/webapp.

Our tablets/phones are sending a request every 60 seconds to retrieve all activate orders. (It echo's out the data regardless of change --- and currently I'm actually echo'ing full div's and markup, which obviously I will change so that the client side deals with that).

I've been looking into Server Side events because we'd like to have more LIVE like updates, but I don't quite understand how to trigger a send event and it actually being more efficient.

From my research the way I'd have to do it is check the database every so many seconds to see if data changed (if the data didn't change it would have to hold current data in memory to compare with new data in the next loop).

Or do I just send ajax request every 10 seconds? What is more efficient/scalable? Do I need to switch to node.js for server side? (I do like the simplicity of PHP)

Or maybe websocket? I don't know near enough about it.

We've got a Quad core Xeon VPS
CPU seems to be mostly at 10-20% usage with occasional spikes to 80% usage during busy times.
Our CPU load sits around 2.5 - 3.5 for 15 minute average but I have seen 7-8 occasionally.

Honestly, I don't understand enough to make any decisions so I'm hoping for some input/advice.

Preach to me... and go!

My Answer:
First of all, congratulations on the success of your business! It's great that you're looking to improve your code and optimize your server load.

In terms of handling live updates and reducing server load, Server-Sent Events (SSE) or Websockets would be more efficient than sending frequent AJAX requests. SSE allows the server to push updates to the client without the client needing to constantly poll the server for updates. Websockets provide a full-duplex communication channel between the client and server, allowing for real-time communication.

In your case, considering the number of devices sending requests every 30-60 seconds, switching to SSE or Websockets would definitely help reduce server load. With SSE, you can have the server push updates to the clients only when there is new data available, reducing the need for constant polling.

As for whether you need to switch to Node.js for server-side handling, it really depends on your specific requirements and preferences. Node.js is known for its ability to handle real-time applications efficiently, so it could be a good option if you decide to go with Websockets. However, PHP can also handle SSE and Websockets effectively with the right setup.

In conclusion, I would recommend exploring SSE or Websockets for handling live updates in a more efficient and scalable manner. Consider the specific needs of your application and the level of real-time interaction required before making a decision on whether to switch to Node.js or stick with PHP. Good luck with your code refactoring and server optimization!

Rate this post

5 of 5 based on 2111 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us