cap-js-community / websocket

Exposes a WebSocket protocol via WebSocket standard or Socket.IO for CDS services
https://www.npmjs.com/package/@cap-js-community/websocket
Apache License 2.0
9 stars 1 forks source link

How to Broadcast event only to the Sender? #3

Closed vishalsharma190596 closed 1 week ago

vishalsharma190596 commented 1 month ago

Hi Team & @oklemenz2 ,

We are trying to use this WebSocket plugin in our Chat app to handle the streaming of Chat responses coming from OpenAI to the UI in streams.

We were able to use the plugin in a POC and raise WebSocket event from the CAP function. But we want to broadcast the event only to the sender client and not to all the other clients connected to the Server. We tried to use the Identifier concept using the annotation "@websocket.identifier" but what we figured from the code is that it works the other way around to exclude the Identified sender client and broadcast to other clients.

Please let us know how we can broadcast the event only to the Sender client and ignore all other clients.

Appreciate your help.

Thanks & Regards, Vishal Sharma

oklemenz2 commented 1 month ago

Hi,

yes this should be possible. Assuming you are in a ws-aware context, e.g. handling an socket event via an CDS operation handler.

Then you should have access to the ws client facade emit via:

req.context.ws.service.emit(event, data)

Maybe I expose this as part of CDS event emit in future, if this is common use-case.

Can you check.

Best regards,

Oliver

vishalsharma190596 commented 3 weeks ago

Hi @oklemenz2,

Thank you for your suggestion. I tried to implement this and it works as expected when there is a Websocket-enabled function that is called from UI to CAP, in the handler of which we call the Websocket-enabled event using your suggested method req.context.ws.service.emit(event, data).

But in my scenario from UI, we want to call a normal CAP action that is not WebSocket enabled, in the handler of the action I want to emit the WebSocket based Event to the caller of the Action only.

Please let us know if this is possible. Much appreciate your help.

Thanks & Regards, Vishal Sharma

oklemenz2 commented 3 weeks ago

Ok, I see. This is then currently not possible. But I will see, that I implement a solution based on the identifier concept, to only send to this identifier client. Stay tuned…

oklemenz2 commented 1 week ago

Include User is implemented with ws 1.2.0: https://github.com/cap-js-community/websocket/releases/tag/v1.2.0 Please check.