aspose file tools*
The moose likes Servlets and the fly likes HTML5 Server-Sent Events & Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML5 Server-Sent Events & Servlets" Watch "HTML5 Server-Sent Events & Servlets" New topic
Author

HTML5 Server-Sent Events & Servlets

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Hi folks, I am looking into some new functionality that comes with HTML 5 and looking for any advice or experience people may have in relation using Server Sent Events in conjunction with Servlets.

I want to create a simple example of a HTML page connecting to the server using server sent events. The server pushes simple messages to the client, which results in the messages getting displayed in the HTML page.

My question is around creating a Servlet to send the events. The only thing I can think of is to create an infinite loop - i.e. the doGet method never returns. Is there any other support for Server Sent events in Servlets?

Ideally I would like to be able to register on the server side that the client wants to receives events, and then be able to broadcast events whenever I wish from the server side.

Any ideas?



SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Hi,

there are existing systems out there doing this stuff

for example, check COMET

http://en.wikipedia.org/wiki/Comet_(programming)

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

olivier dutranoit wrote:Hi,

there are existing systems out there doing this stuff

for example, check COMET

http://en.wikipedia.org/wiki/Comet_(programming)



Thanks. Not sure how this helps with my original query of using Server-Sent Events that come with HTML 5? I want to use Server-Sent Events - not Comet
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Oh i see :-)

didn't see the html5-thing
Now i see what you are asking.
I got your question totaly wrong :-)
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

I googled some.
Must say, its pretty new to me.

But one thing that comes up is, not to do it trough servlets that are keeping open the connection...

They all talk about java websockets.

for example i found this : http://jwebsocket.org/

i will try to experiment with this too!
if you have a working example, can you share it?

Thanks!
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

olivier dutranoit wrote:They all talk about java websockets.


Websockets are something totally different. I'm interested in Server-Sent Events.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

http://dev.w3.org/html5/eventsource/ seems to have both good examples and good explanation. The only trick to getting their examples to work is to have your server send the Content-Type header as "text/event-stream" for the event itself.

If you want to use a servlet to send the event stream (since this is the Servlets forum), simply make sure to call setContentType(...) on the response object prior to any other output. Here's how you would do their first example with the doGet(...) method of a servlet:



Again, the only "trick" to the code above is that you're setting the content-type. You could do this just as easily with JSP (or PHP, or perl CGI, etc, etc).


OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

Pete Nelson wrote:the only "trick" to the code above is that you're setting the content-type.


Thanks Peter. Not sure if you read my original question - but it you've just posted the exact same link I posted and the same type of example. So not sure how this helps answer my question?

What I am asking is...Is there any other support for Server Sent events in Servlets.

Ideally I would like to be able to register on the server side that the client wants to receives events, and then be able to broadcast events whenever I wish from the server side.

The example I have shown involves using an infinite loop in the doGet method - i.e. the doGet method will never return. I don't want to use an infinite loop. I want my doGet method to return normally, and for server to be able to broadcast messages to the client at any possible time.
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Sean Keane wrote:Thanks Peter. Not sure if you read my original question - but it you've just posted the exact same link I posted and the same type of example. So not sure how this helps answer my question?


Do you mean
Sean Keane wrote:Is there any other support for Server Sent events in Servlets?


I guess maybe what you need to clarify is what you mean by "any other support". You can do "Server-Sent Events" from a servlet by setting the proper content-type header, and following the formatting rules shown in the URL we've now both mentioned. What else are you expecting servlets to do for you?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

The example I have shown involves using an infinite loop in the doGet method - i.e. the doGet method will never return. I don't want to use an infinite loop. I want my doGet method to return normally, and for server to be able to broadcast messages to the client at any possible time. Anyone got any ideas on how to achieve this?

Has anyone experience of using Server-Sent events with Servlets? What set-up did you use? I'm guessing that having an infinite loop in the doGet method is not the way to go?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HTML5 Server-Sent Events & Servlets