Nodejs+socketio+redis implementing front-end message real-time push

Source: Internet
Author: User

Nodejs+socketio+redis implementation of front-end message real-time push 1. Back-end section sending Redis messages

You can refer to this implementation (directly using Jedis)

Http://www.cnblogs.com/binyue/p/4763352.html

2. Backend section: receiving REDIS messages
var Redis;IfProcess.Argv.Length<=2){Redis=Require' Redis ').CreateClient ();}Else{Redis=Require' Redis ').CreateClient (6379,Process.argv[2]);}var io=Require' Socket.io ').Listen5678);VAR clients={}Redis.Subscribe' Console ');Redis.On' Message ',function(Channel,Data{if (channel=="Console"){args=Json.Parse (data);Forvar sIn clients){Clients[s].Emit' Event_ '+Args.Id,Args.Message;}}});Io.On' Connection ',function(Socket{var address=Socket.Request.Connection.Remoteaddress+":"+Socket.Request.Connection.RemotePortConsole.LogNewDate ()+' Client connected ['+Socket.Id+"] "+ address); clients[Socket.Id= socket;Socket.On' Disconnect ',function(){Console.Log"Discopnnect"+This.Id+"-----"+This.Request.Connection.Remoteaddress+ ":" + this .  Request. connection. remoteport) ; Delete clients[socket.  ID]; }); socket. On (' event ', function(message){ });}) ; 

Package.json

{  "name" : "real-time",  "description" : "providing real-time message push for ",  "version" : "0.0.1", "dependencies" : { "socket.io" : "1.2.1", "redis": "0.7.3", "socket.io-client": "1.2.1" }}
The front-end section receives messages and displays

Using the socket.io-1.0.6

var socket = io.connect(beeper_url);    socket.on(‘event_‘ + event_id,function(data){ callback(data);})

Nodejs+socketio+redis implementing front-end message real-time push

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.