19

The backend of my web application receives updates from several clients. When such an update happens it should be communicated to all other clients.

How can I initiate an update from the server to all web browser clients when my backend is updated?

I'm using JBoss, JSF and the Spring framework.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Pradeep Gamage
  • 585
  • 4
  • 8
  • 21
  • 2
    One small remark, although Spring itself is a great framework, you probably don't need it with JBoss as this already includes technology that directly overlaps with Spring (e.g. EJB and CDI). – Arjan Tijms Dec 29 '11 at 11:05

3 Answers3

18

See similar Stack overflow quetion : WebSockets vs. Server-Sent events/EventSource

I'm assuming, as DarthVader did, that your frontend is a (generally) stateless HTML page of some sort. Something in a browser. If you want all clients to be pushed changes automatically, you have three options:

Comet: (deprecated)
Comet is essentially making AJAX requests that have no request timeout limit. You make the request, and it sits there and streams data through it as is neccessary. This can be done with hidden iFrames or standard XMLHTTPRequests (which jQuery can wrap for you). You can read more about this method here.

Long Polling:
Essentially, you use the javascript setInterval method to continuously poll your server for changes. Simply set an interval that does a standard AJAX GET request to the server, and upon every success, update your page accordingly.

Browser APIs

  • HTML5 WebSockets
    Using any type of Event-Based backend (Twisted, EventMachine, node.js, etc) makes WebSockets the ideal solution. Simply have all clients register with the backend, and upon a submit from any given client, push the changes to all other clients. You can read more (and see a nice example) of WebSockets on this page. Browser support => canIuse

  • Server-sent event (SSE)
    With server-sent events, it's possible for a server to send new data to a web page at any time, by pushing messages to the web page. These incoming messages can be treated as Events + data inside the web page. Browser suppport => canIuse

Ronan Quillevere
  • 3,699
  • 1
  • 29
  • 44
Mike Trpcic
  • 25,305
  • 8
  • 78
  • 114
4

When you say front end, you are talking about stateless http client.

You cant push anything from your web servers to http or stateless clients.

The "trick" to do this if using asynchronous calls from front end to your back end, periodically.

Think about gmail, how do you think it displays that you have an email when you recieve a new email. You browser contantly, sending Asynch calls to gmail servers, if and when there is a new message, it displays it.

So Clients are stateless. use Ajax.

is this clear?

DarthVader
  • 52,984
  • 76
  • 209
  • 300
0

There are a couple of ways to go around this.. The way it should be in the future is following standards like Websockets

For now you are stuck with Comet which is essentially sending a request to the server and keeping it open (not signaling a response end) and just streaming data through it (Parking the request they call it). Or periodic polling, where you just do an AJAX request to the server every predefined interval to ask if the server has something new to say. Needless to say the first work around requires streaming support on both the server and browser but is more efficient in most scenarios.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28