In Server Sent Event, it always send the same response to all the client. but what i want to know is, How to send response to an only one client using java.
this is my event which define inside sw.js (SSE)
var eventSource = new EventSource("HelloServlet");
eventSource.addEventListener('up_vote',function(event){
console.log("data from s" , event.data);
var title = event.data;
self.registration.showNotification(title, {
'body': event.data,
'icon': 'images/icon.png'
})
});
I want to show this notification only to an specific user. not for everyboday. HelloServlet is my servlet and it contain this,
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
String upVote = "u";
String downVote = "d";
for(int i=0; i<10; i++) {
writer.write("event:up_vote\n");
writer.write("data: "+ upVote +"\n\n");
writer.write("event:down_vote\n");
writer.write("data: "+ downVote +"\n\n");
writer.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writer.close();