File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Is it possible to design the system for invoking and get invoked by its clients? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is it possible to design the system for invoking and get invoked by its clients?" Watch "Is it possible to design the system for invoking and get invoked by its clients?" New topic
Author

Is it possible to design the system for invoking and get invoked by its clients?

Haytham Douaihy
Greenhorn

Joined: May 10, 2012
Posts: 14

We used to develop Servlets to be able to handle Client sessions, where they, for example in case of a web browser, invoke a service using a form or a link, then the servlet do its work and reply to the client.
this is the typical client-server model that i'm used to work on..
my question is, is it possible that the servlets update its clients without the client invocation? let's say, the system's data is changing each second, so my question that is it possible that some parts of the client web interface to be updated each second automatically for example?
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Haytham Douaihy wrote:... is it possible that the servlets update its clients without the client invocation? let's say, the system's data is changing each second, so my question that is it possible that some parts of the client web interface to be updated each second automatically for example?


Haytham,

I think the direct answer to your question is no because in general a browser must be expecting something from the server of it to update.

I can think of a couple of different ways for you to accomplish the same result but both involve the client requesting an update.

The easy way is use the refresh header (See: http://www.w3schools.com/tags/att_meta_http_equiv.asp). This just tells the browser to refresh the page on a set interval.

More complicated but also more flexible is to use AJAX, which is a Javascript technology to request (usually) small amounts of data and then you can use javascript to update only a part of the page.

Joe


It's not what your program can do, it's what your users do with the program.
Haytham Douaihy
Greenhorn

Joined: May 10, 2012
Posts: 14

Ok Joe, thanks for the reply,

Do you think online trading systems, or some parts of the social networks just use ajax or refresh() method for always updating their sessions? especially the trading systems that have the data change instantly in fractions of the second...

thank you

Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Haytham Douaihy wrote:Ok Joe, thanks for the reply,

Do you think online trading systems, or some parts of the social networks just use ajax or refresh() method for always updating their sessions? especially the trading systems that have the data change instantly in fractions of the second...

thank you



Hytham,

I now nothing about online trading systems but IMHO AJAX and REST would certainly be the better technologies to constantly update small sections of a page. Small being a relative term.

I've done a bit of AJAX to do autocompletion from a central db and it wasn't bad at all but I'm certainly no expert. You could use a javascript timer to ping the server to see if an update is needed. Even over a crappy Internet connection that should be fractions of a second and a low burden on both server and client.

Joe
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Haytham, you may want to consider websockets. A websocket is basically a persistent connection between the client browser and server over a single TCP socket. So the client starts the connection and then, whenever the server has something it wishes to broadcast (e.g. updated data), it simply uses the connection to write updates to the client (e.g. as a text message or JSON String etc). That is, the server is able to send data to the client without any further client requests. I use Dojo on the client and Tomcat's websocket implementation on the server to do this in one of my applications.

You may find some of the links below helpful. The first one explains the concept of websockets and the other two show how to use Tomcat to implement websockets:
http://www.infoq.com/news/2008/12/websockets-vs-comet-ajax/
http://www.tomcatexpert.com/blog/2012/05/01/how-apache-tomcat-implemented-websocket
http://muha.net/blog/8/
Joe Areeda
Ranch Hand

Joined: Apr 15, 2011
Posts: 318
    
    2

Thanks Vanessa,

I often learn more by broadcasting my ignorance than reading.

I even have a potential use for web sockets that I was going to have clients poll.

Best,
Joe
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Be aware that web sockets are only supported in a limited number of modern browsers. If you need to support IE9 and earlier, you are up the creek.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vanessa Danin
Ranch Hand

Joined: Oct 04, 2004
Posts: 34
Joe, your response was certainly not ignorant! As Bear mentioned websockets are not supported in older browsers and is only supported in the current version of IE. My particular application is intranet based and we can control which browsers are run so web sockets are suited to our environment.
Haytham Douaihy
Greenhorn

Joined: May 10, 2012
Posts: 14

Thank you all for your replies,

i will have now to do a POC on how to use websockets. thank you Vanessa for the links... i was always wondering how applications broadcast changes to their clients! maybe Facebook also uses them in comments, and messages, and whole news feeds..

Best Regards
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16055
    
  21

A final note. No one said (I think) specifically, but HTTP is a request/response protocol with a strict 1/1 request-to-response correspondence, so no, you can never have the server send unsolicited responses to a client.

Websockets are a way to get around this, but the technology is very new and nonstandard on both the client and server sides. The "live update" sites you see with dynamic flight schedules, stock quotes, and so forth are almost invariably using AJAX. Although if the full screen is being updated, then it's more likely that they're using an HTTP refresh meta-tag. These technologies work pretty much everywhere. Also, since they work by automating periodic requests, you don't have to worry about network resources being eaten in an attempt by the server to push data to places that don't want to hear about them anymore.

Incidentally, by using a browser-neutral javascript support package such as jQuery, you can greatly easy development of AJAX-refreshed web pages.


Customer surveys are for companies who didn't pay proper attention to begin with.
Haytham Douaihy
Greenhorn

Joined: May 10, 2012
Posts: 14

Hi Tim,

First, thanks for your valuable information!
Second, do you mean real time websites don't use web-sockets for instant update of parts of their clients, and all they use are simply 'periodical ajax calls' from the clients to update their data? is there any concerns in using this method? maybe synchronization concerns, client-load, server load...?
And if that's the case, then what is the best use of web-sockets introduced by Vanessa & Bear? Maybe it's main usage are in SOA architectures where server updates all their registered clients? or am i wrong ?

thank you
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16055
    
  21

General-use web applications have to play to the lowest common denominator of web technology. Since websockets are not well standardized yet, nor supported by all browsers, that means that AJAX is the closest thing you can get. Even AJAX was more than you could hope for back around the time of IE4, though. So someday...

The best use for websockets is where all participants are on a known and compatible technology. This is usually only truly possible for internal networking, unless you are OK with excluding people.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to design the system for invoking and get invoked by its clients?