3
votes

i have created simple chat application. and therefor i have used node.js i have seen many simple example over net, all are saying that the code is working fine. but when i am trying that code, it is not give me proper result.

it is throwing error "discarding transport"

i have read following pages : 1) Examples in using RedisStore in socket.io 2) http://www.ranu.com.ar/2011/11/redisstore-and-rooms-with-socketio.html 3) socket.io broadcast function & Redis pub/sub architecture 4) I'm receiving duplicate messages in my clustered node.js/socket.io/redis pub/sub application and many more...

following is the my code :

server side code : app.js ~~~~~~~~~~~~~~~~~~~~~~~~~

var app = express.createServer();
app.listen(process.env.PORT);

var io = require('socket.io').listen(app);

var store = redis.createClient();
var pub = redis.createClient();
var sub = redis.createClient();

var io = require('socket.io').listen(app);

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', 3);
    io.set("transports", ["jsonp-polling", "xhr-polling", "websocket", "flashsocket", "htmlfile"]);
    io.set("polling duration", 10);
    io.set("flash policy server", false);
    io.set("connect timeout", 500);
    io.set("reconnect", true);
    //    io.set('close timeout', 60 * 60 * 24); // 24h time out
    io.set('close timeout', 25);
    io.disable('heartbeats');
    io.set('heartbeat interval', 20);
    io.set('heartbeat timeout', 60);
    //    io.set("polling duration", 10);
    //    io.set("heartbate timeout", 30);
    //console.log("blabla");

    //var RedisStore = require('socket.io/lib/stores/redis');
    //io.set('store', new RedisStore({ redisPub: pub, redisSub: sub, redisClient: store }));
    //io.set('store', new RedisStore());
});

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

    client.on("OnConnect", function (data, fn) {
        console.log("socket id : " + client.id + " connected !!!");
    });

    client.on('disconnect', function () {
        console.log("\r\nmanish from server->disconnect");
        //        client.broadcast(client.sessionId + " disconnected")
        client.emit('user disconnected');
        sub.unsubscribe("chat");
        sub.quit();
    });

    sub.subscribe("chat");
    sub.on("message", function (channel, message) {
        console.log("message received on server from publish : '" + message + "'");
        client.send(message);
    });
  });
});

client side code : index.html ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        this.Connect = function (nick, room) {
            socket = io.connect('http://XXX.XXX.X.XX', { transports: ['jsonp-polling', 'xhr-polling'] });
            Nickname = nick;
            Room = room;

//            setInterval(function () { socket.emit("keep-alive", null) }, 20 * 1000);

            socket.on('connect', function (data) {
                socket.emit('OnConnect', { nick: nick, room: room }, function (response) {
                    $("#board").append("<p>" + response.msg + "</p>");
                });
            });

            socket.on("message", function (msg) {
                alert("message received on client ...");
                $("#board").append("<p>" + msg +"</p>");
            });

            server.on("listening", function () {
                var address = server.address();
                console.log("server listening " + address.address + ":" + address.port);
            });

            socket.emit("message", { msg: msg, nick: Nickname }, function (response) {
                $("#board").append("<p> send message : " + Nickname + ": " + msg + "</p>");
            });

        };

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ but it is not working for me.

means i am run this app in one browser (let say Firefox, and send some message to 2 nd connection, let say IE )

but is shows following error in console.log

debug: setting request GET /socket.io/1/jsonp-polling/Th9U-Zci8cVb5Wfwl24Y?t=1345123231804&i=0 debug: setting poll timeout debug: client authorized for debug: clearing poll timeout debug: jsonppolling writing io.j0; debug: set close timeout for client Th9U-Zci8cVb5Wfwl24Y socket id : Th9U-Zci8cVb5Wfwl24Y connected !!! message received on server from publish : 'msg 1' debug: setting request GET /socket.io/1/jsonp-polling/Th9U-Zci8cVb5Wfwl24Y?t=1345123231804&i=0 debug: setting poll timeout debug: client authorized for debug: clearing poll timeout debug: jsonppolling writing io.j0; debug: set close timeout for client Th9U-Zci8cVb5Wfwl24Y socket id : Th9U-Zci8cVb5Wfwl24Y connected !!! message received on server from publish : 'msg 1' debug: fired close timeout for client Th9U-Zci8cVb5Wfwl24Y info: transport end (close timeout) debug: discarding transport

1

1 Answers

2
votes

Socket.io rooms work out of the box you no need to subscribe to redis or anything.

Try to on your custom connection function to

client.on("OnConnect", function (data, fn) {
    console.log("socket id : " + client.id + " connected !!!");
    client.join(data.room);
});

Also you don't need to unsubscribe from the room, but if you do it should look like this

client.on('disconnect', function () {
    console.log("\r\nmanish from server->disconnect");
    client.emit('user disconnected');
    client.leave("chat");
});

Sending a message to a chatroom is done with client.broadcast.to('chat').emit('message');

You can read more about rooms at https://github.com/LearnBoost/socket.io/wiki/Rooms

Good Luck!