Async data fetching from Java WebService - java

I'm implementing a client-server GPS application. Client side is a J2ME midlet that sends GPS location via HTTP/XML to a Java Webservice (Tomcat servlet). The servlet stores positions in SQL database. The other client app is a web browser that can login and see the actual position of midlet using Google Maps. This client is written using GWT. All is deployed on the same Tomcat container. Now I'm wondering how to dynamically update current position in webrowser Google Maps of the mobile so that the client can see how the mobile moves. How to do it in GWT - should I create a timer object in GWT client and asynchronously send HTTP request to server for getting the actual positions? Or is there any mechanism in AJAX/GWT to notify client (web browser) about the data update?
Thanks
Dominik

Regardless of all the talk about "push", the standard HTTP model is still one where the client has to ask the server for updates.
In a GWT app, you should use the well-supported Ajax functionality to request small-granular updates (polling, I guess) from the server at regular intervals (5 seconds, maybe?) and use the newly obtained information to update your map info.

There is a detailed article on the GWT incubator web site about Server Push and how to achieve it with GWT. From the article:
Explains Server Push, sometimes known as 'comet', and how you can achieve this with GWT.
Then, if you take a look at the comments, you can find interested related information and open implementations as gwt-comet, GWTEventService ...

I agree with rok.
If your deployment is going to be small enough to be able to support one permanent connection per web browser client go with Server Push/ Hanging RPC/ Long Polling or whatever you want to call it.

Related

How to send targeted push notifications to a specific client?

I just got started with Java Web Application Development and I've been developing a Web Application with Java Servelts using Apache Tomcat where customers can book appointments with hair stylists. Now I want to add a feature where stylists are notified by a push notification in real time once a client makes an appointment. I did my research and I found out the following ways:
Long Polling
Server Sent Events/ Event Source
Web Sockets
Since the necessary communication is asynchronous from the server, I feel that the Server Sent Events would be the right choice.
I came up with an idea where the book event generates a notification which is stored in a separate table. A thread monitors the data source for any new notifications and notifies the targeted receiver once a new nofication is found.
The issue I am facing here is how to identify the necessary active client out of several clients and target the data to it.
I've been looking everywhere in the internet and I couldn't find any Java Servlet and Tomcat implementations for reference.
Is it possible to implement my idea using just Servlets in a Tomcat container?
If there is a better approach to this problem, please let me know.

Android app communication with web server?

I am coming from java based web application background where client is thin client i.e. browser and all UI pages(HTML,CSS,JS) along with
data comes from server. But with recent read on net about android based mobile apps and based on Q/A on stack overflow came with this understanding
Android app with backend(where data is fetched from server) is like two application communicating with each other on http. When I say two app , these
are one on mobile which is java(android) application and second is on server which is generally exposed through web services which can be in any language
like java/dot net. Is it correct ?
If yes I have one doubt when comparing it with java server based web app This is in regard of where UI pages . In web application,
UI views are returned from server in the forms of HTML. But I am confused where UI pages resides in android app. Is it generally on client side(all ui pages
are downloaded with app . With different events app serves the UI from client side itself but data comes from server) or they are served from server ?
If from server are they served again as HTML or there is some other format that android better gels with ?
May be android app generally should not expect any UI pages from server which means downloaded app should have all UI pages with it.
I think the UI is still on the client while the content for filling of the UI is from the server. This content is transported in HTTP(s) protocol, therefore in HTML format as you said. But one can use other protocols to get the response from the server I guess.

update java arraylist changes dynamically in html

I have a array list in java which contains notification messages which will be displayed in a html page.
Is there way that when the array list gets updated(i will update this array list from the listeners in smack chat library) the list in html also gets updated simultaneously without refreshing the page.
I am not interested in hitting the controller using any scheduler that will update the list every x minutes.I need the list to be updated simultaneously.
I have heard that we can use web sockets on this.I have no idea on this.Is that the way to go.If so kindly provide the tutorial for the same.
There are many technology to make chatting application over web browser.
HTTP server push - nothing but HTTP streaming, which uses WebSocket API and browser should support HTML5, server can push the message to the client.
Pushlet - it takes the advantages of the persistent HTTP connections. and communicate using that connection.
Long polling - pull the data using ajax and jquery or any ajax based technique.
Flash XMLSocket - when your browser does not support HTML5 that time this can be used.
I worked with same kind of application where , we developed all the technique depending upon the user browser support, we invoke the appropriate technique.
you can get more info about WebSocket here

Push APIs like gmail, twitter and facebook does

I want to create an application using pushing mechanism. i.e. without user interaction we can push the messages to the client when some thing is happend in the server, similar to what gmail is done for their emails and facebook recent activity messages.
How can i implement this one using java.
Please help, Thanks in advance.
One technique to achive this is taking advantage of the long-polling method.
The client opens up a connection to the server, but instead of instantly returning the result, the server holds the connection for a period of time and delays his response.
If something happens, that causes the server response to trigger (new mail, new instant message ... ), the server finally answers to the client. Or, in the case the request runs in timeout, the server just passes an empty result.
In both cases, the client processes the response and instantly makes a new request, for the whole thing to start again.
In Java, the client side may do simple requests. The server side should be properly threaded, and the timout should not be set too long.
Also, please have a look at this SO thread:
Each webserver/appserver has a pool of threads, say 10 threads for processing web requests, if all those threads will go into 'sleep' no other web request will be serviced until one of those 'sleeps' exists.
What you need is called as Comet aka Reverse Ajax, there are multiple such implementation both paid and free.
Depending upon programming language you choose you can choose one of the following:
Tomcat Reverse Ajax.
Jetty Comet.
DWR
Streamhub (Paid but has a community edition)
APE. (Free not available for Windows)
Atmosphere (Used in Primefaces)
CometD by Dojo.
Or just use Pusher API. You use their REST service to publish events and Pusher then delivers these events to all clients via websockets. The API is easy to use and supports multiple channels.
Though a late answer , here is my take on how to do server push.You can use the socket-io framework coupled with nodejs.The socket-io client has to listen to events and the socket-io on the nodejs ( server ) can emit ( push ) the messages , the client ( socket-io in webpage ) picks it up and can do whatever it wants.There is a catch,you should know a little bit of javascript.The socket-io protocol is transport agnostic too !
Here are the links
An excellent writeup on using socket-io and nodejs
Socket-io
nodejs

Migrating Ajax web application to web socket

I think I'm just missing a little detail that is preventing me from seeing the whole picture.
I have a web application which use ajax request every x time to update client with new information or tasks.
I also have a long running process on the server which is a java computation engine. I would like this engine to send update to the client.
I am wondering how to migrate my web app to using websocket. Probably phpwebsocket or similar. Can my server 'decide' to send information to a specific client? It seems possible looking at the php-websocket.
Can my java backend long process use the websocket server to send notification to a specific client. How? well I can say that my java app could use a class that could send over websocket instead of http.
But how the websocket server knows to which client to send the 'info'. I am puzzle by all this. Any document that explain this in more details? It seems that the websocket could create an instance of my web application.
Thanks
Your server, which will have an arbitrary number of active client sockets, decides which ones to write to (possibly in response to input from the user).
phpwebsocket (which is still very rough around the edges) has a User class with $id, $socket (this is the underlying TCP socket), and $handshake fields. You could extend that class with additional metadata about the User (e.g. a computation identifier). Or you could use an array mapping from computation id to User.
Perhaps when Java computation n finishes, you can look up the socket associated with that computation, and write to its socket.

Categories