Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io for real time application

So I have another project that I built a few months ago. It currently uses jquery with ajax and makes a call to a php script every 30 seconds. The php script queries mysql and passes back the results to display. In this application the person has the ability to add/edit/remove records from the db and when they do the list of results from the db needs to update the list they see. So far it seems to do the job, but looking down the road the db will get bigger and it will need to be more in real time, meaning not just refresh every 30 seconds but as instantaneous as possible.

Would socket.io be the answer to this? Would I simply use socket.io with nodejs to create a server and emit events to query my db and return the results to display? Then on the client side just have a function that calls the server socket script like every second? Would that be the correct path to using socket.io? If so would there be a concern with this process if I ran it every second with regards to server resources?

like image 445
John Avatar asked Jun 26 '11 17:06

John


1 Answers

Would socket.io be the answer to this?

My answer is that socket.io / node.js is much better at dealing with real-time applications then PHP in the current form.

Then on the client side just have a function that calls the server socket script like every second?

In node.js you should not poll ask for the information, but instead you should have the information pushed(use redis pubsub or node.js events for that) to you. I advice you to have a look at a pubsub snippet from me on stackoverflow.com to explain myself a little bit. For the code to work you will need to install socket.io 0.6.x(0.6.18) because the latest socket.io 0.7.x has a slightly different API.

If so would there be a concern with this process if I ran it every second with regards to server resources?

Like I said before use pubsub semantics. You could for example use:

  • redis pubsub
  • events
like image 84
Alfred Avatar answered Sep 23 '22 03:09

Alfred