6

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?

John
  • 9,840
  • 26
  • 91
  • 137

2 Answers2

3

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:

Community
  • 1
  • 1
Alfred
  • 60,935
  • 33
  • 147
  • 186
  • @Alfred This is where I get confused. So your saying I dont need to run a script at some interval on the client or server side? I can set it up on the server side where it only pushes data when there is an update? Say a row was added or deleted in the db the script can be setup to detect this and push to the client without using something like setinterval? – John Jun 26 '11 at 18:55
  • @John that is exactly what I am saying :). You should play with Redis's pubsub from the provided cli to see it in action.. When the database mutates you publish the changes to desired channel interested... – Alfred Jun 26 '11 at 20:40
  • @Alfred Im reading your example and I believe I understand it, sort of. Basically I would subscribe them to a channel, my question is what is a channel and how would I create the channel to do something like query a db and return results? Or is that not what a channel would be? – John Jun 26 '11 at 22:14
  • In the `redis pubsub` => http://redis.io/topics/pubsub article I posted above I think explains it clearly(better than I could?) what Pub/Sub is. For the rest the way I learned redis is by just firing redis-server and playing with it running a couple `redis-cli` instances and play with it. In my opinion the best way to learn something is by playing with it and that's why redis + redis-cli is awesome. I would also recommend you to read Simon Willison's tutorial => http://simonwillison.net/static/2010/redis-tutorial/ – Alfred Jun 26 '11 at 23:19
  • @Alfred Yeah I read the page and it made me cross eyed. I guess I dont get it, yet. – John Jun 26 '11 at 23:30
  • Haha okay that is possible. My advice is to just issue those pubsub related commands. Open lets say three `redis-cli` windows from terminal. `publish` to a channel(name) in one terminal and subscribe to that same channel(name) in one or both other terminal windows. Hope that you will get it soon... P.S: I have asked this question to answer what pubsub is exactly => http://stackoverflow.com/questions/6487394/explain-what-pubsub-is-and-how-to-use-it-in-redis. I will also try and post an explanation later(not right now, because it is already 2:00 am in the morning)... – Alfred Jun 26 '11 at 23:41
  • I found this video hopefully explaining how redis/pubsub works. I am still watching it :P => http://nosql.mypopescu.com/post/5070178301/building-publish-subscribe-apps-with-tropo-and-redis – Alfred Jun 26 '11 at 23:46
  • yup I think the video should give you a better picture what pubsub is... Haha I always thought tropo.im was also cool, but now after watching this screencast I am going to play a little more with tropo.im :) – Alfred Jun 26 '11 at 23:49
  • Even one tutorial more explaining pubsub I guess => http://rediscookbook.org/pubsub_for_synchronous_communication.html – Alfred Jun 26 '11 at 23:58
  • @Alfred just watched that video, now Im understanding it better. Hell I didnt know you could use php as well! – John Jun 27 '11 at 01:04
  • Ok after some research redis is a db that also has a pub/sub ability. I also looked at Faye but Im liking the idea of using redis and not just for this but to work along side memcache for caching purposes on my site. The only lapse Im seeing is the ability to authenticate the user? Which Faye has the ability to do that. But I really like redis php ability as well. – John Jun 27 '11 at 19:55
  • @John you should look into redis :). I should have probably explained redis a little better(read the site). Using node_redis the performance is going to be very GOOOD. Redis is very fast and I think even more powerful then memcached.. – Alfred Jun 27 '11 at 21:50
  • @John there is enough information on SO to handle authentication. It has even improved with 0.7.x because we now have an `authorize`. Also read https://github.com/LearnBoost/Socket.IO-node/wiki/Configuring-Socket.IO – Alfred Jun 27 '11 at 21:52
2

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?

If you want something realtime. The structure might be different.

First you need a middle man which help you communicate between browser and server.

Solution 1 : Request every 30 seconds to get realtime (Same as your current method)

Solution 2 : http Streaming. Once request has make to the server. Server can continue send response to browser. Due to many security browser issues, socket.io has born. Socket.io provide many method streaming, htmlfile stream,xhr request,flash...

Second, you need server to accept the connection and make a long poll services. Thank god ! socket.io has done this part for you.

Thirdly, the most important person ! That is data.

Solution 1 : Every request/trigger Call db or nosql database (SQLserver,mysql,Mongodb). Believe me ! Your db going die soon.

Solution 2: Messaging services. Services like Redis pub/sub, rabbitQ. It simply use subscribe and publish message in queue. It use particular protocol to publish message and they did not store message like db. So its can send message >100 k request /seconds ! Super fast.Wow ! Is it use this services can solved realtime solution ? Unfortunatly that a bit hard. Why ? Because use this kind of services you cannot store data, SQL query to customize your data need.

Solution 3 : In-memory process.Redis,memcached. Memory is super fast! You can store your final display results in memory. Memory can reached >100 k request/seconds.

Conclusion. To build a really realtime web application like facebook or tweeter. We need to apply all solution above + some cheat. Example, facebook friend notification. When you get friend news update, it use messaging services to publish alert to all friends (Max 5000 people).Because heavy publish will slow down message performance. After that cache (Memcached) the notification message to reduce calling db(mysql). How about we can get group message with over 1 million people ? Of course we dun publish to all over 1 million people. But we ask 1 million people to call our latest group messages which store in memory.

  • Reminder : Scaling your server is important to achieve the realtime goal. Good luck !
Community
  • 1
  • 1
user717166
  • 690
  • 2
  • 10
  • 17
  • I'm building an report real-time application to analytics. My app will interact with mysql database to analytics data with some features such as analytic number of user register/login/chat/call on my app, then display on dashbroad view. How to do that with socket.io? Any suggesting? – Loint Jul 05 '16 at 03:44