Redis with nodejs + socketio and python, post event fires twice?

I have installed a nodejs server along with a simple python script with redis-py.

I have this on my nodejs server:

var http = require('http');
var server=http.createServer(onRequest).listen(3000);
var io = require('socket.io')(server);
var redis = require('redis');
var fs= require('fs');
var sub = redis.createClient();
sub.subscribe('channel');


function onRequest(req,res){
    var index;
    fs.readFile('./index.html', function (err, data) {
    if (err) {
        throw err;
    }
        index = data;
        res.writeHead(200,{'Content-Type':'text/html'});
        res.write(index);
        res.end();
    });

};

var sockets=[];
io.on('connection', function(socket){
  console.log('a user connected');
  sockets.push(socket);
  console.log(sockets.length);
  sub.on('message', function(channel, message){
    console.log(message);
    sockets[0].emit('chat message',message);
    });

  io.emit('chat message', "enter nickname");
  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});

      

This is a simple test when I was trying to figure out why my messages are being sent multiple times.

I figured out that it sub.on('message')

runs twice for every message I send from python. Why?

The python code is pretty simple:

import redis
r=redis.StrictRedis()
pubsub=r.pubsub()
r.publish('channel',"HHHHHHH")

      

+3


source to share





All Articles