how to run socket.io and now.js without any issue?

174 views Asked by At

I'm trying to run an application which uses socket.io for some functionality and nowjs for other some functionality?When i run application both the socket.io keep on clashing. please suggest what i have to do?

chat module

var io = require('socket.io');
var users = {};
exports.initialize = function(server){
       io= io.listen(server,{ log: false });

        io.sockets.on('connection', function(socket) {


            socket.on('send message', function(data){
                io.sockets.in(socket.room).emit('message', socket.user, data);
            });



            socket.on('messageandsender', function(user){
                var rcvr = user.receiver;
                var sndr = user.sender; 
                io.sockets.emit('msgandreceiver',{'message': user.msg, 'rvr': rcvr, 'sndr': sndr});
            });         


            socket.on('disconnect', function(){
                    delete users[socket.user];
                io.sockets.emit('update',users);
                if(socket.user != undefined)
                socket.broadcast.emit('info', 'SERVER', socket.user + ' has gone offline');
                socket.leave(socket.room);
            });
        });
    };

paint module

var nowjs = require("now");
exports.startnow = function(server){
var everyone = nowjs.initialize(server);
nowjs.on("connect", function () {
    console.log("Joined: " + this.user.clientId);
nowjs.on("disconnect", function () {
    console.log("Left: " + this.user.clientId);
    nowjs.getClient(this.user.clientId,function(){
        console.log(this.now.name);
    });
    updateList();
});

everyone.now.distributeMessage = function (message) {
    everyone.now.processIncomingChatMessage(this.now.name, message);
};

everyone.now.distributePaintCommand = function (paintCommand) {
    everyone.exclude(this.user.clientId).now.processPaintCommand(paintCommand);
};

everyone.now.setName = function(){
    userlist.push(this.now.name);
    console.log(userlist);
    everyone.now.updateUserList(userlist);
};

var userlist = [];

updateList = function(){
    userlist = [];
    everyone.getUsers(function (users) {
        for (var i = 0; i < users.length; i++){
            nowjs.getClient(users[i],function(){
                console.log(this.now.name);
                userlist.push(this.now.name);
            });
        }
    });
};

everyone.now.getUserList = function() {
    this.now.updateUserList(userlist);
};

});

};

in server.js

var server = http.createServer(app).listen(app.get('port'), function(){
});
chat.initialize(server);
paint.startnow(server);

I've added paint model in this case both the sockets from paint and chat module clashes giving following connection warning several times.but unable to connect but when i comment one either chat or paint it works fine

WebSocket connection to 'ws://localhost:3000/socket.io/1/websocket/6gMr-tmuaA0kJnNQrIQz' failed: WebSocket is closed before the connection is established. socket.io.js:2438
1

There are 1 answers

0
user4082518 On BEST ANSWER

you can't run both thing simultaneously because both try to require socket @ same port try nginx for node app