Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding socket.io's emit and on?

Tags:

During development, it helps me greatly to be able to see what packets arrive and gets sent. This is possible on the server side with logger. On the client end, however, there is no logger. I find myself to be littering console.log all over the place.

Is it possible to override socket.emit and socket.on with console.log(arguments)? If I can override this at the before my socket, it would be really elegant.

Somebody advised me to override the Parser instead.

What's your 2cents on this?

EDIT

I tried Kato's suggestion and wrote the following:

var _origEmit = socket.emit; socket.emit = function() {    console.log("SENT", Array.prototype.slice.call(arguments));   _origEmit.call(socket, arguments); }; 

This works. However, Not so much with socket.on. My strategy is to wrap each callback with a console.log. If you know python, it's kind of like putting function decorators on the callbacks that console.log the arguments.

(function(socket) {  var _origOn = socket.on; socket.on = function() {    var args = Array.prototype.slice.call(arguments)     , handlerType = args[0]     , originalCallback = args[1];    var wrappedCallback = function() {      // replace original callback with a function      // wrapped around by console.log     console.log("RECEIVED", Array.prototype.slice.call(arguments));     originalCallback.call(socket, arguments);   }    _origOn.call(socket, [handlerType, wrappedCallback]); } 

Any one can point to why monkey patching socket.on is not working?

like image 378
disappearedng Avatar asked Jan 12 '12 09:01

disappearedng


People also ask

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

Does Socket emit have callback?

Event CallbacksThe socketio. Server. emit() method has an optional callback argument that can be set to a callable. If this argument is given, the callable will be invoked after the client has processed the event, and any values returned by the client will be passed as arguments to this function.

Is Socket.IO full duplex?

info. WebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser.

How does Socket.IO work internally?

A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving: A session id that will be given for the transport to open connections. A number of seconds within which a heartbeat is expected ( heartbeat timeout )


2 Answers

To override socket.on you actually need to override socket.$emit.

Following example works both client and server-side (tested on socket.io 0.9.0):

(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);   }; })(); 
like image 194
nme Avatar answered Sep 27 '22 17:09

nme


Works, tested:

var _emit = socket.emit;     _onevent = socket.onevent;      socket.emit = function () { //Override outgoing         //Do your logic here         console.log('***', 'emit', arguments);         _emit.apply(socket, arguments);     };      socket.onevent = function (packet) { //Override incoming         var args = packet.data || [];         //Do your logic here         console.log('***', 'onevent', packet);         _onevent.call(socket, packet);     }; 
like image 25
Alexander Arutinyants Avatar answered Sep 27 '22 17:09

Alexander Arutinyants