Joenel de Asis
Joenel de Asis

Reputation: 363

Socket.io and Redis Pub/Sub not working

Hello guys i would like to figure out what is the error in my code, my code is about socket.io and redis pub/sub it is my first time to try this, I hope you can help me guys.

This is my index.html

<!doctype html>
<html>
    <script src="/socket.io/socket.io.js"></script>
    <script> 
        var socket = new io.Socket();
        socket.connect();

        socket.on('connection', function (socket) {
            console.log('Connected');
        });

        socket.on('disconnect', function (socket) {
            console.log('Disconnected');
        });
    </script>
    <center>
    <h1>Test Page</h1>
    </center>
</html>

This is my app.js

var redis = require('redis');
var app = require('http').createServer();
var io = require('socket.io').listen(app);
var client = redis.createClient();
var pub = redis.createClient();
var sub = redis.createClient();

app.listen(1234);

io.sockets.on('connection', function (socket){
    sub.on('subscribe', function (channel){
        pub.publish('Pub','Test Message 1');
        pub.publish('Pub','Test Message 2');
        pub.publish('Pub','Test Message 3');
    });
    sub.on('message', function (channel, message) {
        console.log(channel + ':' + message);
    sub.unsubscribe();
    pub.end();
    sub.end();
    });
    sub.incr('Channel Test');
    sub.incr('Pub');
});

I hope you can help me fix this code. Thanks in advance guys.

Upvotes: 3

Views: 6303

Answers (2)

Cybermaxs
Cybermaxs

Reputation: 24556

I can see many errors in your code :

  • in index.html, you should connect to http://localhost:1234/, because it's defined in your server code.
  • var client is not used in app.js
  • sub is never subscribing to something. You need to subscribe to a channel
  • A connection in subscriber mode can not send commands to redis : only commands that modify the subscription set are valid
  • sub.incr will never publish a message : you have to call publish.
  • do not call pub.end() or sub.end() because the connection will be closed.
  • do not add an handler to event message under connection event : memory leak

I don't know exactly what do you want to do but here is an updated version :

index.html

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

        socket.on('connection', function (socket) {
            console.log('Connected');
        });

        socket.on('disconnect', function (socket) {
            console.log('Disconnected');
        });
    </script>
    <center>
    <h1>Test Page</h1>
    </center>
</html>

app.js

var redis = require('redis');
var app = require('http').createServer();
var io = require('socket.io').listen(app);
var pub = redis.createClient();
var sub = redis.createClient();

app.listen(1234);

sub.subscribe('Pub');//subscribe to Pub channel
sub.on('message', function (channel, message) {
    console.log(channel + ':' + message);
});

io.sockets.on('connection', function (socket) {
    pub.publish('Pub', 'New Connection');
    pub.incr('Channel Test');   //increment 'Channel Test' but do not publish messages
    pub.incr('Pub');            //increment 'Pub' but do not publish messages
});

Upvotes: 3

Tony Chen
Tony Chen

Reputation: 1844

I didn't see you set redis as store in socket.io server. Some example I did for your reference:

// start up express server along with socket.io
var express = require('express');
var server = express();
var socket = require('socket.io');
var io = socket.listen(server);

// socket.io
io.set('store', new socket.RedisStore);

// set-up connections...
io.sockets.on('connection', function(socket) {

        io.emit('an event sent to all connected clients');

        socket.on('some-event', function(rooms) {
            ...
        });

});

You can read document links below:

  1. Configuring Socket.IO
  2. socket.io github site

Upvotes: 1

Related Questions