Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add broadcast-packet send-socket-packet events #78

Closed
wants to merge 1 commit into from

Conversation

DmytroDrachov
Copy link

@DmytroDrachov DmytroDrachov commented Nov 9, 2021

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.

@darrachequesne
Copy link
Member

@ddrachov well, that's interesting, thanks for this 👍

There is another API suggestion here: socketio/socket.io#4145. Would it suit your use case?

@DmytroDrachov
Copy link
Author

@darrachequesne Hey, the socketio/socket.io#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 socketio/socket.io#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.

darrachequesne added a commit to socketio/socket.io-website that referenced this pull request Nov 15, 2021
@darrachequesne
Copy link
Member

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?

@DmytroDrachov
Copy link
Author

DmytroDrachov commented Nov 15, 2021

@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?

@darrachequesne
Copy link
Member

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.

@darrachequesne
Copy link
Member

This feature was implemented in version 4.5.0:

socket.onAnyOutgoing((event, ...args) => {
  console.log(event);
});

Thanks for the work on this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants