-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Adding support for middlewares intercepting outgoing events #4885
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
Conversation
bperel
commented
Dec 18, 2023
Is this PR basically allowing to add event emit interceptors? It could be a great way to allow acknowledgement caching, in the same way that axios-cache-interceptor does it for Axios requests :-)
Is this PR basically allowing to add event emit interceptors? It could be a great way to allow acknowledgement caching, in the same way that axios-cache-interceptor does it for Axios requests :-)
I haven't played with axios around yet. But from what I read from the axios-cache-interceptor docs the idea of this PR is similar to the interceptors, even tho the implementation varies a little bit. I based my implementation on the socket.io socket middlewares implementation so it works the same (unlike axios cache interceptors where inbound interceptors are FILO and outblound are FIFO).
The main point stands of being able to hook into and filter properly in and outbound requests is the same.
Uh oh!
There was an error while loading. Please reload this page.
The kind of change this PR does introduce
Current behavior
Currently middlewares are limited to hook before the server aknowledges a clients message, but it can't hook on the client acknowledging from the client.
Currently data validation logic needs to be rewritten in the logic of every emited with acknowledgements, that may result in a lot of boiler plate code.
New behavior
Introduced the useOutgoing((event: [string, ...any[]], next: (err?: Error) => void) => void) method.
It is behaving much like regular use() does, but before the messages are being sent from the server to the client.
(They do run before the catch all listeners and before the acknowledgement callback gets popped to the ack manager in order to be able to edit the event, or abort it).
Example:
Other information (e.g. related issues)
I disscussed other option briefly in this issue #4882.
For short, the tricky part about this, is that one might want access to the context of the event that originated the ack repsonse whilst consuming what the client acknowledged. Else it would be of no use hooking there, except for logging purposes I guess.
This would be a solution that doesn't disrupt current usage of Socket.IO, and we have access to the context of the event.