Background Java stores data in a Redis server as data changes, and the foreground responds with node and Redis, using Socket.io to update the browser data.
I. Local development test installation of Redis server
Redis official Windows 64-bit address
1. Unzip the zip package, cmd Enter, enter Redis-server redis.windows.conf run Redis server
2.redis Command line tool (REDIS-CLI), double-click to open, enter help view instructions
3. Install Redis (CNPM i redis--save) in the project directory
Second, configure config.js in node
/** Config */var options = {rds_port : " 6379//port number rds_host : ' 127.0.0.1 //server IP to connect to a server Redis rds_pwd Span class= "Pl-k" >: ' "//password rds_opts : {}//settings }; module. exports = {options:options}
Third, App.js
ConstRedis= require ( "Redis" const options = require ( "/config" redisclient = redis. CreateClient (options. Rds_port,options. Rds_host,options. rds_opts); var server = require ( ". server (app);
Iv. Configuration Reids
Redisclient. On (' Connect',console. Log ("Redis Connect successredisclient. Subscribe ("Diew");});
I'm doing this demo using Ajax to invoke commit, then write to Redis,redis to publish, then trigger the Redis subscription, read the data in Redis, send to the client via Socket.io
Write to RedisRouter.All"/commit",function (req,res,next) {var commits = json. Stringify (req.query. data); var name = req.query. Data. name; redisclient. set ( "Bidinvest", Commits,redis. print); redisclient. publish ( "Diew", commits);})
V. Connecting sockets
Io. On (' connectionfunction (socket) {redisclient. On (' Messagefunction ' (console. Log (' Socketio connectionsocket. Emit (' diewmsg', msg);} )
VI. Client Code
Init socketvar socket=Io.Connect' Http://127.0.0.1:3000/‘);Socket connectionSocket.On' Connection‘,function () {Console.Log' Connection setup for Socket.io‘) });Channel for socket subscriptionSocket.On' Diewmsg‘,functionmsg) {var objmsg=Json.Parse (msg);var random=parseint (Math.Random ()*6)+1;The returned datavar html=‘‘; Html+=' <li class= ' row list-group-item "><div class=" Col-md-2 col-xs-12 ">‘; Html+=' ‘+random+'. jpg ' alt= "Llalalalla" class= "Img-thumbnail text-center Avatar" >‘; Html+=' <p class= ' text-center ' >‘+objmsg. Name+ ' </p></div> '; Html+= ' <div class= "col-md-10 col-xs-12" >+= ' <p> ' +objmsg. Txt+ </p></div></li> $ ( "Commit-all"). Append (HTML); })
Note: Source code please visit GitHub https://github.com/dai1254473705/node-socket-redis
Summary: This is just a simple demo, if you need more features can go to Socket.io (https://socket.io/docs/) and Redis (Https://github.com/NodeRedis/node_redis) View the document on the
For example, set Socket.io connection node authentication, incoming parameters, and so on, if the online server use Nginx, need to ip_hash; Ensure that the connection is the same port; If you put it in an online environment,
Io. Connect (' http://127.0.0.1:3000/'); you can write var socket = IO () directly, this will automatically identify the link address, the default transports is WebSocket, If the link is unsuccessful, it automatically changes to the polling mechanism, such as:
But the local development time does not have this problem, only on-line environment to encounter (the online environment socket and Redis relatively complex, but the same problem), do not know is nginx or other aspects of the problem, know that Daniel can come out to popularize knowledge
Nodejs Redis Socketio