Using NodeJS for real time live updating view using database
I hav开发者_如何转开发e been playing around with Nodejs and now wanted to know if i could create live update to a view/page as shown in this tutorial here
The example above would apply to all users on site, what i want is to target my updates to certain users.
Do i create a array storing all the client sockets, a socket is created when the users logs in.
Another thing how can i update the webpage or the view if something has updated in the database do i poll the server every second?
I am using MySQL has database, should i used Redis instead?
EDIT: one more question I was wondering how can nodejs check if the database field/s have been updated or changed and than update the view or webpage?
thanks
Do i create a array storing all the client sockets, a socket is created when the users logs in.
If you would use socket.io module for managing connection between clients and server, then you don't have to worry about the structure or stored clients since it would be managed for you in the background. It offers also various fallbacks (including long polling) if client browsers do not support advanced transports like WebSockets.
Another thing how can i update the webpage or the view if something has updated in the database do i poll the server every second?
DO NOT poll the server every second, since transports like long polling and WebSockets were introduced to AVOID this. Since you would have persistent connection between client and server with socket.io (which are using techniques and technologies like long polling or WebSockets), you can rather easily create evented system which updates or notifies certain client(s) about the change at the time when it happens.
I am using MySQL has database, should i used Redis instead?
Redis is very good Key/Value store for real-time, frequently updated data which do not require complex querying. If you need advanced querying support for your data, then try to look at MongoDB for example.
精彩评论