Communication between two web browser windows - java

I need to create web application which must have two separate web browser windows to comfort usage. And events on each of them must cause action on another. The app will be on html5, perhaps with websockets on client side and Java with glassfish on server side. Is there any way to cross browser window communication or some another solution for this problem?

The best way to solve this problem is to store the information on the server side and have the web application communicate with the server (with JavaScript I expect).
You can have the web page "poll" the server every set period (eg 5 seconds) to see if there is an event or message to handle.

I used postMessage before - but not very heavily, worth a shot.
See post and demo at: http://robertnyman.com/2010/03/18/postmessage-in-html5-to-send-messages-between-windows-and-iframes/
Also see http://benalman.com/projects/jquery-postmessage-plugin/ for jQuery postMessage

Yes, I described a lot of ways here:
How is it possible to share single js resource between browser tabs?
If the browser support is not so important for you, then you can use shared webworkers. The next option to use storage event to exchange data, one implementation is intercom.js for that. The most supported way to use cookies BNC Connector does that.

Related

How to send an asynchronous event from a Java server to a web site with JavaScript?

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?
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:
Is there some way to PUSH data from web server to browser?
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).
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)
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.

Can a Java Applicaton and a JavaScript running in a browser communicate with each other?

I have a JavaScript embedded in a website, running in a normal browser, and I have a Java Application running on the same machine. I am looking for a way have both communicate with each other.
I assume that creating files in the local file system from inside JavaScript running in a browser is out of question.
The only way I came up with would be to use a server that both programs can send messages to, and from which they poll for new messages.
Is there any other other way to accomplish this?
A couple of ways I saw in practice:
You Java Application may listen to some local port which your JS will access for instance via XHR. You'll need to mind cross-site scripting (your JS may need to be loaded from that local URL), but this is doable. The easiest would probably be to run an embedded HTTP server.
Your Java Application may be registered as a protocoll handler in the OS. Then JS would open links registered with the application thus sending data to it.
As #PavelHoral is pointing out, CORS is a way to workaround same-origin policy.
JavaScript inside a browser can only make AJAX requests or communicate with browser plugins if they provide some additional JS interface.
Local HTTP connection
One option is to listen for HTTP connections in your Java application (does not have to be servlet).
You will need to handle CORS correctly.
Central server
Other option is to have a central server to which both your JS and Java code will connect.
Java applet
Another option is to have Java applet. When running in privileged mode you can do pretty much anything (maybe you can convert your Java application to Java applet).
You will need to handle applet security here (e.g. signing applet with trusted certificate).
Please refer to this question for direct sockets communications from within javascript. You'll need a HTML5 browser, and likely this won't "just" work or necessarily be a good idea if you want to publish this on the WWW.
How to Use Sockets in JavaScript\HTML?

Play Audio on Java EE web application on client side (browser)

We are trying to play audio on the client machine. It is a web application based on Java EE, wherein due to some event happening at server side, the client should ring a bell or something of that sort.
I am not aware of any such thing. Alternative is to use
Html5
, but then it's become browser dependent that the browser should be Html5 compatible. I cannot enforce this requirement onto the client.
I went through
Red5
but found it not very useful..
Please advise
There are two parts to the problem:
playing sound in a browser
the browser being notified of events on the server
For playing sounds, and to support a wide range of browsers, you have a number of options. HTML5 <audio> tag and Plugins (Flash, Java applet). You could also use a Javascript library such as Yahoo Media Player to make this easier. A starter is here.
The second problem is how to notify clients when an event on the server occurs. This could be done with AJAX calls polling the server. Since you are using Java EE, this could be made a bit more efficient using Asynchronous Servlets. You could also go down the WebSocket path with this as well, though there can be issues through proxy servers with this.

GWT database-access without RPC

I'm using GWT for a web-app and I need to access to a mySql-database. There will be only one client (The app is used on a iPad localy). Is there any way to access the database without RPC? I'm looking for a possibility to direkty query a database.
Thanks!
There are 2.5 reasons you cannot use gwt to directly access MySQL.
Reason #1.
GWT is compiled into Javascript. You need to open a socket to the database server. GWT does not allow you to open a socket. In fact, no unaugmented browser (before advent of html5) is able to open a socket. But you can open a socket using Flash actionscript, or HTML 5 javascript.
Reason #2.
OK, let's say you used HTML5 sockets. And you spent 6 months writing in Javascript a JDBC connectivity. But, your websocket would still need to address a servlet on the server which would help your websocket establish a persistent connection - and mysql is unable to perform such an establishment.
Reason #3.
SLD - SOP restriction:
(Second Level Domain Same Origin Policy)
Standard browser restricts its pages to only be able to request for, and to include, content from within the same second-level domain (SLD) as the server that provided that page to the browser. Top level domains (and top-level and a half) are such as .com, .org, .net, .me.us or .co.uk. So, domain names such as google.com, fbi.gov, mit.edu are second level domains. While, mail.google.com would be a third-level domain. Therefore, GWT would work only within the confines of an SLD. Your web server must also be accessible at the same SLD as your mysql server.
SLD-SOP and tunneling requirement is to close a security hole that could have allowed any tom-rick-or-mary to log into your system thro your browser. Tunneling is always required for a browser to connect to a server other than a http server. Tunneling is when a browser exploits the web server as a yenta (yiddish for busy-body/go-between/match-maker) to get to another server.
You have no choice but to use GWT-RPC. Perhaps you don't wish to use RPC, then you could use RequestBuilder, or Script-Include or RequestFactory. But they are all still diverse means of tunneling. http://h2g2java.blessedgeek.com/2011/06/gwt-requestbuilder-vs-rpc-vs-script.html.
There is one reason why you can connect to your database server from your gwt client:
Your database server must run httpd connection engine. That is, your gwt app would access the db server thro http. I am not familiar with which relational database has a http access available. Most probably, you would have to query thro xml or json.
However, a company I had worked for created our own http service to allow "direct" client access. "direct" is a misnomer because we used tomcat. It is stil tunneling. Any database company that offers "direct" http access is still tunneling. Tunneling - no escape from it.
You could augment the browser with Flash and write a Flash application rather than using GWT. If direct access is so essential to you, you would have to abandon GWT and develop in Flash and run a httpd engine for your database server.
GWT is ultimately Javascript. As noted at Are there JavaScript bindings for MySQL?
, there is currently no way of accessing MySQL from Javascript.
Therefore you can't access it from client-side GWT code.
AFAIK it's not possible, and even if it were, it would be a really bad idea. Are you sure you actually need a database? Maybe something like gwt-client-storage would be more appropriate.
EDIT
Your database would we publicly accessible and open for any sort of attacks.
EDIT 2
This may even be a better solution, as it offers support for accessing the HTML5 Database API and is targeted to iPhone/iPad.
gwt-mobile-webkit
If you were even successful in doing so, in short, doing a CTRL + U on the browser would make your database name, username, password, tables names etc visible... And done, any developer curious to know your code has a way to hack anything and everything in your server.
I think it's not possible, I mean, if you want all your data stored in DBs. I mean, GWT compiles into javascript and javascript executes on the client (typically a web browser).
If you want to access data stored somewhere (by some mean) in a server, then you have no option but RPC. If I were you, I would stop thinking in client-server paradigm (GWT was developed with that in mind). Perhaps some embedded database like H2 and then hold connections through JDBC.

Periodic Query or Other Implementation: Java and Http

Consider, several users are using my Java Application.
I am using Http Common Apache to GET/POST from a Http Server to get various user data.
There is a module in this client Application which triggers current Page no. of the Document (user1 opened). I want other Users(User2, User3,....UserN) to know the Page no. User1 opened.
Should all users listen/query the HTTP server in a time interval always ? I think it is not a good idea.
How can I implement this so that Load on server is minimized.
Long Polling:
Clients open a connection with your server and the server sends response only when user1 has changed the page (Considering your example). This will work great if the number of clients for your applications are not huge.
User Query:
If dynamic update of page number on your application is not a hard requirement, querying the page number will be a good idea by providing a link would be a good option. This will improve the server performance.
You need to use Comet (long-polling) in your server application to avoid periodic polling. J2EE 6 will support it. Developing with Comet and Java describes how to make it in J2EE 5.
Some other server-side platforms are not well suited for Comet (for example PHP).

Categories

Resources