Reputation: 405
I have a Restful service where a user can get a new SSE broadcast. The code looks like this:
SseBroadcaster sseBroadcaster = new SseBroadcaster();
@GET
@Produces(SseFeature.SERVER_SENT_EVENTS)
@Path("/getEvent")
public EventOutput getSseOutput(@Context HttpServletResponse response) {
EventOutput eventOutput = new EventOutput();
sseBroadcaster.add(eventOutput);
return eventOutput;
}
What I'd like to do is when a new EventOutput is added to the broadcaster, send a welcome message to that EventOutput. However, everyone already listening to the broadcaster will also receive the message. Does anybody know of a way to send an event to a specific EventOutput?
Upvotes: 2
Views: 1418
Reputation: 28928
You call eventOutput.write(event);
, where event
is an object of OutboundEvent
.
Making the OutboundEvent
appears to require a rather tedious builder class. See the example in the documentation. I believe this is the minimum code to send a welcome message:
final OutboundEvent.Builder eventBuilder = new OutboundEvent.Builder();
eventBuilder.data(String.class, "Welcome!");
final OutboundEvent event = eventBuilder.build();
eventOutput.write(event);
Upvotes: 2