You can either override the .emit()
method (saving the original so you can call it) or if you control all the code that calls .emit()
, then just make your own method that adds the timestamp to the payload and then calls .emit()
.
To patch the original .emit()
, you could do this:
(function() {
var origEmit = Socket.prototype.emit;
Socket.prototype.emit = function(msg, data) {
if (typeof data === "object") {
data.timeStamp = Date.now();
}
return origEmit.apply(this, arguments);
}
})();
To create your own emit method that all your code could use, you could do this:
Socket.prototype.emitT = function(msg, data) {
if (typeof data === "object") {
data.timeStamp = Date.now();
}
return this.emit.apply(this, arguments);
}