2

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

Anshul
  • 92
  • 7
  • http://stackoverflow.com/questions/15713080/html5-server-sent-events-and-multiple-clients-without-using-comet. Almost same requirement but not that complex. Just multiple users logged in to some view and i should be able to send notification to some specific users. I cannot use atmosphere or any 3rd party framework. – Anshul Jul 21 '15 at 11:43

3 Answers3

2

Probably, the solution I see, would be using two different EventSource to address the specific users and standard users. That means two different urls. In the sample code there, I use a query param to distinguish the two kinds of users but you can also use different paths (depending on your use-case).

That's usually what do our users when they want to turn their JSON API into a push API via streamdata.io (streamdata.io is based on SSE) (disclaimer: I'm a developer @streamdata.io).

With the streamdata.io JS SDK, you could also use headers to distinguish your users. Note, that SSE does not support custom headers: you need to use query params.

If ever your use-case relies on a JSON API and you want to push data, you can use streamdata.io ;).

ctranxuan
  • 785
  • 5
  • 9
  • I am trying to run this example in Tomcat 8.0. But i am getting 404 error. Then i added com.sun.jersey jersey-servlet 1.19 After that i am getting 500 error. Can you check what am i doing wrong. – Anshul Jul 23 '15 at 05:04
  • The sample uses SpringBoot which uses itself an embedded Tomcat 8 to run the app. So, executing the jar will start an embedded Tomcat 8. But if you wish to deploy a war into a Tomcat 8, you need to do some [modifications](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file) and update the `index.html` with the proper eventsource urls. I've made a branch `tomcat-deployment` in the github repo. Just run `mvn clean install` to produce the war and deploy it into your Tomcat. See the README.md. – ctranxuan Jul 23 '15 at 07:06
  • can you provide link for the tomcat-deployment branch – Anshul Jul 23 '15 at 07:48
  • While looking for solutions i can upon this one. I am new to maven so i am not able get it to work. Some problem with pom.xml https://github.com/jersey/jersey/tree/master/examples/sse-item-store-webapp can you look at this one please. – Anshul Jul 23 '15 at 07:51
  • For checking out a git branch, you may be interested by [this](http://stackoverflow.com/questions/1783405/checkout-remote-git-branch). As far the maven stuff, you might post another stackoverflow question for your issue... or adapt the code of the Jersey sample with mine. – ctranxuan Jul 23 '15 at 08:40
1

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 ».

ctranxuan
  • 785
  • 5
  • 9
0

you can create different events on the server side and then the client would listen only to the events that are relevant for them.

server side

echo "event: special_user\n";
$msg1="This is special user";
echo 'data: {"msg": "' . $msg1 . '"}';
echo "\n\n";

echo "event: normal_user\n";
$msg2="This is normal user";
echo 'data: {"msg": "' . $msg2 . '"}';
echo "\n\n";

special user client side

var evtSource = new EventSource("sender.php");
evtSource.addEventListener("special_user", function(e) {
var obj = JSON.parse(e.data);
var r_msg = obj.msg;

normal user client side

var evtSource = new EventSource("sender.php");
evtSource.addEventListener("normal_user", function(e) {
obj = JSON.parse(e.data);
var r_msg = obj.msg;
Anush
  • 151
  • 1
  • 5