duplicate message received on redis sub

I am using express 3x, node.js and redis. when i as publishing message then 1 have receive this message 2-3 times in subscribe. (e.g. when i am refreshing my browser, message receive increase by 1 each time) . below is my code.


server side : ~~~~~~~~~~

var express = require('express'),
    http = require('http')

var redis = require('redis');
var redisCli = redis.createClient();
var redisPub = redis.createClient();
var redisSub = redis.createClient();

redisCli.on("error", function (err) {
    console.error("\r\n Error generated from redis client ", err);
});
redisPub.on("error", function (err) {
    console.error("\r\n Error generated from redisPub ", err);
});
redisSub.on("error", function (err) {
    console.error("\r\n Error generated from redisSub ", err);
});

var server = http.createServer(app)
  , io = require('socket.io').listen(server);

server.listen(process.env.PORT);

app.configure(function () {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.set('view options', { layout: false });

    app.use(express.favicon(__dirname + '/favicon.ico', { maxAge: 2592000000 }));
    app.use(express.bodyParser());
    app.use(express.cookieParser());
    app.use(express.session({ secret: "myKey", store: new RedisStore({ maxAge: 86400000, client: redisCli }), cookie: { maxAge: 86400000} }));
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/static'));
});

io.configure(function () {
    io.enable('browser client minification');  // send minified client
    io.enable('browser client etag');          // apply etag caching logic based on version number
    io.enable('browser client gzip');          // gzip the file

    io.set('log level', 1);
    io.set("flash policy server", false);
    io.set("transports", ["jsonp-polling", "xhr-polling"]);
});

io.sockets.on('connection', function (client) {
    console.log("server - redisSub.subscribe from io.on.connection");
    redisSub.unsubscribe();
    redisSub.subscribe("announcement");

    redisSub.on("message", function (channel, message) {
        io.sockets.emit('announcement', message);
    });

    client.on('disconnect', function () {
        redisSub.unsubscribe("announcement");
        redisSub.quit();
    });

});

app.post('/PublishMessage', function (req, res) {
    redisPub.publish("announcement", req.body.users);

    res.setHeader('Cache-Control', 'max-age=0, must-revalidate, no-cache, no-store');
    res.setHeader('Connection', 'keep-alive');
    res.contentType('application/json');
    res.setHeader('Expires', new Date().addYears(-10));
    res.json({ result: 'ok' });
});

Client side ~~~~~~~~~

    this.socket = io.connect('http://XXX.XXX.X.XXX/', { transports: ['jsonp-polling', 'xhr-polling'] });
    this.socket.on('connect', function () {
        alert("client - Socket client connect");
    });
    this.socket.on('announcement', function (msg) {
        alert("clientside - announcement ");
        var nUsers = parseInt($('#Summary>article>p:last').text(), 10) + parseInt(msg, 10);
        $('#Summary>article>p:last').text(nUsers);
    });

================================================================= So, any one guide me for the same !!! thank you very much.

I have never used socket.io, but it looks to me like you're over complicating things with your connection handler.

Inside the handler, it doesn't seem like you're reacting to the connection (like emitting a "user connected" event) or modifying the behavior of the individual socket connection in any way.

What you are doing, is repeatedly subscribing and unsubscribing the one redisSub client. I could be wrong here, but I don't think you need to or should be doing that.

Rather you should sub "announcement" once, outside of the connection handler, as you don't need to sub/unsub this global client on every connection. Like:

// Move this subscription outside of the connection handler, and you shouldn't
// have to continue to sub/unsub or otherwise manage it.
redisSub.on("message", function (channel, message) {
    io.sockets.emit('announcement', message);
});

// Since you're not reacting to connections or doing anything with individual
// connection sockets, you don't really have anything to do in this handler.
io.sockets.on('connection', function (socket) {
  // if you ONLY wanted to emit to this socket, you'd do it here
  //socket.emit("announcement", "just for this connection")
});