Closed DmytroDrachov closed 2 years ago
@ddrachov well, that's interesting, thanks for this :+1:
There is another API suggestion here: https://github.com/socketio/socket.io/pull/4145. Would it suit your use case?
@darrachequesne Hey, the https://github.com/socketio/socket.io/pull/4145 is good, but unfortunately it will not intercept events which are going through adapter reason of how adapters implemented.
All of them use the broadcast method of base adapter class which in turn do not use emit of Socket/Namespace/BroadcastOperator classes.
As well https://github.com/socketio/socket.io/pull/4145 will not intercept emit on namespace cause it uses broadcast operator which is using broadcast method of adapter.
The main disadvantage is that there is no single place except engine where we could add our function to intercept all outgoing events.
Hmm, you are absolutely right.
there is no single place except engine
As a matter of fact, you can already do that:
io.on("connection", (socket) => {
socket.conn.on("packet", ({ type, data }) => {
// called for each packet received
});
socket.conn.on("packetCreate", ({ type, data }) => {
// called for each packet sent
});
});
I've included it in the documentation: https://socket.io/docs/v4/server-socket-instance/#socketconn
The data
is encoded though: 2["hello","world"]
if you used socket.emit("hello", "world")
Does it cover your use case?
@darrachequesne That one is good. Did not know about those events. Correct me if I am wrong it will make unreadable data if we decide to use compression?
It could stop working if underlying engine will be changed?
Correct me if I am wrong it will make unreadable data if we decide to use compression?
No, the compression is applied after that.
It could stop working if underlying engine will be changed?
That's unlikely, but in any case, the change would be backward incompatible and would require a new major version. So I think you can rely on those events.
This feature was implemented in version 4.5.0:
socket.onAnyOutgoing((event, ...args) => {
console.log(event);
});
Thanks for the work on this :+1:
Adding new events on adapter instance to be able to add aspects like logger or metric to events which are emitter by socket.io-(*)-emitters.
Could you please point me where could I update that page with new events if you fine with that change.