stomp-js / stompjs

Javascript and Typescript Stomp client for Web browsers and node.js apps
Apache License 2.0
773 stars 81 forks source link

Introduce API for registering message interceptors #290

Open vigie opened 4 years ago

vigie commented 4 years ago

I am connecting to a broker that expects an auth-token header on every CONNECT, SUBSCRIBE, UNSUBSCRIBE and DISCONNECT message. If the token that is sent is bad, it will disconnect the websocket.

I would like a clean mechanism by which to proactively ensure the token I am sending is good, rather than cleaning up the mess in an onError callback. This mechanism must of course be async so that I am able to refresh the token before sending, should this be necessary.

The interceptor pattern is commonly used with HTTP clients. It could be applied here also:

before any message is sent, allow the execution of a chain of client-provided asynchronous functions that can make arbitrary modifications to that frame, before forwarding along the chain, ultimately terminating in the library's client.send method. Something like:

intercept(frame: IFrame): Promise<IFrame>
kum-deepak commented 4 years ago

I get your problem and I believe that the approach you have suggested is proper. Would you like to submit a PR or you would like me to make the change?

vigie commented 4 years ago

I'm not sure how soon I could start, but if I do start I'll let you know here. Please also post here if you (or anybody else) starts work on this