2

I am using socket.io 1.2.0 . In a client browser how to receive all events in a single listener

socket.on("*",fuction(event,data){ 

});

My previous implementation is not working after i uploaded from 0.9 ....

my previous hack for receive all events

     var original_$emit = getSocket().$emit;

        getSocket().$emit = function () {
            var args = Array.prototype.slice.call(arguments);
            original_$emit.apply(getSocket(), ["*"].concat(args));
            if (!original_$emit.apply(getSocket(), arguments)) {
                original_$emit.apply(getSocket(), ["default"].concat(args))
            }
        };
balaphp
  • 1,306
  • 5
  • 20
  • 40
  • I think it's duplicated. See http://stackoverflow.com/questions/10405070/socket-io-client-respond-to-all-events-with-one-handler . A solution: override `socket.on` and `socket.emit`. – creeper Nov 11 '14 at 07:02
  • @creeper $emit is not available in 1.2.0; In that page all they gave result for 0.9... – balaphp Nov 11 '14 at 07:09

1 Answers1

-1

This is not a perfect solution but its working fine....

Code changes server socket.io

/node_modules/socket.io/lib/socket.js

In line no: 128

 if (~exports.events.indexOf(ev)) {
    emit.apply(this, arguments);
 }

change to

if (~exports.events.indexOf(ev)) {
   var arg = ["*",arguments];
   emit.apply(this, arg);
}

In line no: 328

emit.apply(this, args);

change to

args.splice(0, 0, "*");
emit.apply(this, args);

Example:

 var io = require('socket.io')(SERVER);
     io.on('connection', function (socket) {
         socket.on('*', function (event, data) {
         });
    });

Code changes socket.io for (browser javascript to node.js app);

node_modules/socket.io/node_modules/socket.io-client/socket.io.js

In line no: 724

emit.apply(this, arg);

change to

var arg = ["*",ev];
emit.apply(this, arg);

In line no: 847

 if (this.connected) {
     emit.apply(this, args);
 }

change to

 if (this.connected) {
    args.splice(0, 0, "*");
    emit.apply(this, args);
 }

var arg = ["*",ev];

Example:

var socket = io();
socket.on("*", function (event, data) {
});

code changes for require('socket.io-client') (node.js app to node.js app);

/node_modules/socket.io-client/lib/socket.js

In line no: 129

emit.apply(this, arguments);

change to

var arg = ["*",ev];
emit.apply(this, arg);

In line no: 253

 if (this.connected) {
     emit.apply(this, args);
 }

change to

 if (this.connected) {
    args.splice(0, 0, "*");
    emit.apply(this, args);
 }

var arg = ["*",ev];

Example:

var io = require('socket.io-client');
var socket = io(URL,{});
socket.on("*", function (event, data) { });

balaphp
  • 1,306
  • 5
  • 20
  • 40