Search code examples
javascriptbroadcastserver-sent-events

How to use server sent event to broadcast to multiple users using javascript at client end and java at server end


I am writing a web application code using server sent event in which i want to broadcast a message to some specific users. I want to use eventsource at client side and jersey broadcaster at server end. If their any other way to achieve it. Websockets are not supporting in my app due to tomcat version and long polling I cannot use in the application.

Can anyone share example with working code. Thanks in advance


Solution

  • Depending of your use-case, another solution would be to use one EventSource and different messages for the different kinds of users. For instance, standard users could only listen to message of type « standard » and special users could only listen to message of type « special ».

    In this case, it is up to the client side to filter / distinguish which events to listen according to the kinds of users. But since there is only one eventsource, you could have client apps that could listen both of the type of messages.

    So, choosing one or the other solution really depends on the use-case.

    I've committed this solution on the same github. Solution 1 is tagged « v1 », solution 2 is tagged « v2 ».