7

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.

4
  • From where do you sending this message $redis.publish 'rt-change', {hello:'world'} ? Commented Jun 4, 2013 at 15:34
  • 1
    Can you put a log into the redis subscribe callback to make sure that it's triggered ? Commented Jun 4, 2013 at 21:38
  • Do you have any errors in your javascript console on the browser ? Commented Jun 6, 2013 at 17:19
  • Have you tried sending message from node application.js? Put some socket.emit('rt-change', message) inside onConnection callback. Also are you 100% sure there is connection between browser and node app? You set transport to xhr-polling, so in console you should see "heartbeats". Commented Jun 6, 2013 at 17:51

2 Answers 2

11
+500
 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>
Sign up to request clarification or add additional context in comments.

1 Comment

Solved by myself 10 minutes ago, then I jump to stackoverflow to close ticket, and here is you answer :) Thanks anyway.
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.