1

I want to develop a Java server that is able to send messages asynchronously to a client in form of a website with JavaScript. I know that one possibility is using WebSockets, but these are not supported in IE 9.

For transmitting messages from client to server I can use AJAX calls with maybe a RESTful Interface on the server side.

Does anyone have a solution for this?

durron597
  • 30,764
  • 16
  • 92
  • 150
tellob
  • 1,142
  • 3
  • 14
  • 31

4 Answers4

1

This is not how webservers work, most of the time. HTTP Webservers are inherently a request-response architecture:

HTTP functions as a request-response protocol in the client-server computing model. A web browser, for example, may be the client and an application running on a computer hosting a web site may be the server. The client submits an HTTP request message to the server. The server, which provides resources such as HTML files and other content, or performs other functions on behalf of the client, returns a response message to the client.

That said, there are technologies that you can use to do this. Read here about Comet and Reverse AJAX:

Community
  • 1
  • 1
durron597
  • 30,764
  • 16
  • 92
  • 150
1

You better implement your Java server to act as a Websocket server when it's supported by the end user. For the users who does not support Websocket it should fall back to long-polling.

This behaviour will avoid unnecessary overheads due to long-polling communications whenever possible.
The good thing is you don't have to implement all these behavious from the scratch. You can readily embed and use a reliable implementation available open source.
One such implementation is CometD project. The CometD project was available for more than a decade and it has evolved to solve most of the issues.
If you are looking for commercial products, there are many available. One such would be lightstreamer (http://www.lightstreamer.com).

Aviro
  • 1,925
  • 18
  • 27
0

You need to use a design pattern like long polling since WebSockets is not available. Rather than build it yourself you could use a library like SignalR. SignalR is an ASP.NET library but there is a client for Java (https://github.com/SignalR/java-client)

alex
  • 394
  • 1
  • 4
  • 16
0

For anyone who comes across this question more recently, the modern answer (as of early 2021) supported across all browsers (except IE, which even Microsoft has given up on in favour of Chromium-powered Edge) are server-sent events. A most elegant and standardised solution to providing a pub/sub model to web clients.

Jason Etheridge
  • 6,643
  • 5
  • 27
  • 32