Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send data to front end when back end updates [duplicate]

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.

like image 897
Pradeep Gamage Avatar asked Dec 29 '11 05:12

Pradeep Gamage


2 Answers

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

like image 193
Mike Trpcic Avatar answered Sep 17 '22 15:09

Mike Trpcic


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?

like image 37
DarthVader Avatar answered Sep 21 '22 15:09

DarthVader