1

socket.io clientside code can use this

  var socket = io('http://localhost:8888');
socket.on('news', function (data) {

    socket.emit('my other event', { my: 'data' });
});

but i want to intercept all data from the server emitted by socket.io not only news and then process the data. is that possible.

wetlip
  • 133
  • 3
  • 19

1 Answers1

1

What you want is to register for all events emitted by "socket". This issue was widely discussed here. It's a no go in the socket.io code, but commenters suggest some ways to do it.

Your best bet is probably to follow this path :

(function() {
 var emit = socket.emit;
 socket.emit = function() {
 console.log('***','emit', Array.prototype.slice.call(arguments));
    emit.apply(socket, arguments);
};
var $emit = socket.$emit;
socket.$emit = function() {
    console.log('***','on',Array.prototype.slice.call(arguments));
    $emit.apply(socket, arguments);
  };
})();

Please look at the linked topic for more informations.

You may find additional informations here on some other ways to do it..

Community
  • 1
  • 1
sdumetz
  • 552
  • 2
  • 11
  • this doesnt work for me. the clientside socket.io.js has a function which gives access exactly what i want //Socket.prototype.onPacket = function (packet) {}; but how to acces the function. – wetlip Sep 28 '14 at 20:30
  • i use socket.io.js 1.1.0 from [link]https://github.com/automattic/socket.io-client. i hasnt the function socket.$emit as in older versions. – wetlip Sep 29 '14 at 16:25