node.js + socket.io + redis + rails ― RealTime application

匿名 (未验证) 提交于 2019-12-03 08:42:37

问题:

I need to add realtime to my application (Ruby On Rails), so, I think that the better way to do it is to use node.js + socket.io + redis.

I have this application.js file in a backend (node.js)

var app = require('http').createServer(); var io = require('socket.io'); var redis = require('redis').createClient(); var _ = require('underscore')._;  io = io.listen(app); io.configure(function() {     io.set("transports", ["xhr-polling"]);     io.set("polling duration", 10);     io.set("close timeout", 10);     io.set("log level", 1); })  redis.subscribe('rt-change');  io.on('connection', function(socket) {     redis.on('message', function(channel, message) {         socket.emit('rt-change', message)     }); });  var port = process.env.PORT || 5001; app.listen(port); 

And messages.js in frontend

 var socket = io.connect('http://localhost:5001/socket.io'); socket.on('rt-change', function (data) {     console.log(data); }); 

I'm launching application.js with node application.js command and it works!

MacBook-Pro-Zhirayr:rt zhirayr$ node application.js info - socket.io started

But when I'm trying to send message with redis ($redis.publish 'rt-change', {hello:'world'}) from Rails application, my browser doesn't log anything in console. I'm sure, that connection from browser established, cause when I stop node, it throws connection refused error. And I'm sure that connection between redis and node established, cause console.log(message) in application.js logs it. But console.log in browser doesn't log anything.

Any ideas why?

Thanks.

UPD for #Antoine

Added console.log in application.js io.on('connection', function(socket) { redis.on('message', function(channel, message) { console.log('new message from redis'); socket.emit('rt-change', message); }); });

When r.publish 'rt-change', {:hello=>'world'} become executed, node logs this:

new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis new message from redis 

It's strange, node logs 11 times for 1 message.

回答1:

 var socket = io.connect('http://localhost:5001/socket.io'); socket.on('rt-change', function (data) {     console.log(data); }); 

This part of the code does not seem correct, according to the doc on http://socket.io , you should do something like:

<script src="http://localhost:5001/socket.io/socket.io.js"></script> <script>   var socket = io.connect('http://localhost:5001'); </script> 


回答2:

The reason why you are getting multiple executions for a single publish is because of the nested event-handling in your code. I will try to explain this as simple as I can.

When you call

io.on('connection', function(socket) {...}); 

you listen for connection event and add the given function as event handler. It is executed whenever a new user connects. This creates a separate scope, for the execution of the handler each time.

When you call this

io.on('connection', function(socket) {     redis.on('message', function(channel, message) {         console.log('new message from redis');         socket.emit('rt-change', message);     }); }); 

you add listeners for redis subscriber event message for each of the connected users separately. This is why you are getting multiple logs and connected users get multiple copies of same message. You got 11 messages which means there were 11 different users connected at that time.

To solve this you can call once instead of on for listening events. Like this

io.once('connection', function(socket) {     redis.on('message', function(channel, message) {         console.log('new message from redis');         socket.emit('rt-change', message);     }); }); 

This applies to the socket.io clients also. See my previous answers to get clarity on this:

  1. socket.io code structure: where to place methods?
  2. socket.io creates one more connection after reconnecting


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!