Push notification using atmosphere - java

I am using Atmosphere RC 2.4.5(java 1.8, jsp, tomcat 8, and javascript Atmosphere client) for pushing messages from server to the clients. Everything works as expected, and didn't have any trouble with the implementation of it, but now I have doubt about the next thing:
I implemented a feature where admin user can send some notification to all users currently using the app. But if in that exact time while the message was pushing through web sockets, some user clicked link and started navigation to another page. His web socket would be closed and he would never get the message.
Does anyone know how this can be achieved using the atmosphere, so no messages would be lost.
Thanks all.

You can find your solution based on this page from documentation of atmosphere-js :
Sharing connection between Browser's windows and tabs
By default, the atmosphere.js library will open a new connection (based on the available transports: websocket, long-polling, streaming, sse, jsonp or ajax) every time a new window or tab is opened by the Browser. In some case it may be more efficient to share a connection between windows/tabs. To enable the mechanism, all you need to do is to set the shared property of an AtmosphereRequest object:
Here's example :
var request = new $.atmopshere.AtmosphereRequest();
request.shared = true;
request.transport = 'websocket'
$.atmosphere.subscribe(request);
One of these lines can be the solution of your problem :
request.shared = true;

Related

Spring webapp display dynamic content from a database

I have a REST API made with Spring Boot. I can add entities (Persons) to a database using HTTP requests.
I would like to have a web page display those entities dynamically: whenever a new Person is stored in the database, the page displays it on top of the list. No need to refresh the page.
What are the ways to achieve this? Would JSP be enough? Do I need javascript?
The simpler the better.
What are the ways to achieve this? Would JSP be enough? Do I need
javascript?
Yes you need javascript. But you can embed it in JSP too.
This is a solved problem, you will find numerous examples in the web if you search for "websocket spring". Example https://spring.io/guides/gs/messaging-stomp-websocket/
If you are not stuck in JVM-land and RDBMS, I would encourage you to take a look at firebase-angular stack (3-way binding)
Further read
Read this https://stackoverflow.com/a/12855533/6785908
Shameless copy since posting just a link is not advisable as link may break in future
In the examples below the client is the browser and the server is the webserver hosting the website.
Before you can understand these technologies, you have to understand
classic HTTP web traffic first.
Regular HTTP:
A client requests a webpage from a server.
The server calculates the response
The server sends the response to the client.
Ajax Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server at
regular intervals (e.g. 0.5 seconds).
The server calculates each response and sends it back, just like normal HTTP traffic.
Ajax Long-Polling:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which requests a file from the server.
The server does not immediately respond with the requested information but waits until there's new information available.
When there's new information available, the server responds with the new information.
The client receives the new information and immediately sends another request to the server, re-starting the process.
HTML5 Server Sent Events (SSE) / EventSource:
A client requests a webpage from a server using regular HTTP (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection to the server.
The server sends an event to the client when there's new information available.
Real-time traffic from server to client, mostly that's what you'll need
You'll want to use a server that has an event loop
Not possible to connect with a server from another domain
If you want to read more, I found these very useful: (article), (article), (article), (tutorial).
HTML5 Websockets:
A client requests a webpage from a server using regular http (see HTTP above).
The client receives the requested webpage and executes the JavaScript on the page which opens a connection with the server.
The server and the client can now send each other messages when new data (on either side) is available.
Real-time traffic from the server to the client and from the client to the server
You'll want to use a server that has an event loop
With WebSockets it is possible to connect with a server from another domain.
It is also possible to use a third party hosted websocket server, for example Pusher or others. This way you'll only have to
implement the client side, which is very easy!
If you want to read more, I found these very useful: (article), (article) (tutorial).
Comet:
Comet is a collection of techniques prior to HTML5 which use streaming
and long-polling to achieve real time applications. Read more on
wikipedia or this article.

Communicate with a specific browser

I've created a website in JSP with Servlets.
Users can login and they all get a session attribute with their userid.
I created a page with a textbox and button where I can fill in an userid.
Upon clicking the button I want to open a popup (url) on the browser of the specific user with the session userid equals to the userid filled in the textbox. The popup should appear immediately an not require a page request to get shown.
What kind of things I should use for this?
I googled, but couldn't find anything usefull.
Writing a chat client can be quite tricky because you have to send data from the server to the client when a message is sent by another user. WebSockets allows you to do this but it is a pretty new technology which does not work with IE versions below 10. Chrome, Firefox and Safari do support it.
The group of technologies that allow you to use javascript to communicate with the server instead of requiring browser page refreshes is called AJAX.
A library like DWR makes it very easy to do AJAX between Javascript and Java. It also has a feature called Reverse AJAX that allows you to write Java code that executes javascript code on the client. http://directwebremoting.org/dwr/documentation/reverse-ajax/index.html
With DWR you can write this to show a popup on all connected clients:
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
System.out.println("sessions: " + manager.getAllScriptSessions().size());
for (ScriptSession scriptSession : manager.getAllScriptSessions()) {
System.out.println("Sending script to session " + scriptSession);
ScriptBuffer script = new ScriptBuffer("window.alert('hello from reverse ajax server');");
scriptSession.addScript(script);
}
When you need the popup to appear immediately and you can't wait for a page-request, you need some kind of communication channel from server to client which allows pushing messages.
A good tool for this are Javascript WebSockets. A WebSocket is a bidirectional connection between browser and server which is open while the page is open and which can be used by both sides to send data whenever they want.
On every page you need to create a websocket and connect it to a WebSocket Servlet. While the client has the page open, that servlet is able to send a message to the client whenever it wants. You can then handle that message in Javascript to implement a real-time chat application.

Unable to connect to internet in Blackberry device?

I am developing an app where i am using browser field to load html files in it. My code is as follows.
Main.java
//pushing screen to browser field page..
public Main()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new WebViewController());
}
WebViewController.java
BrowserFieldConfig bfConfig = new BrowserFieldConfig();
bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,
BrowserFieldConfig.NAVIGATION_MODE_POINTER);
bfConfig.setProperty(BrowserFieldConfig.JAVASCRIPT_ENABLED,
Boolean.TRUE);
bfConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR, Boolean.TRUE);
bfConfig.setProperty(BrowserFieldConfig.INITIAL_SCALE, new Float(0.0));
bwf = new BrowserField(bfConfig);
add(bwf);
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
bwf.requestContent("local:///html/index.html");
}
}, 500, false);
All my server code & UI design is in javascript & html pages respectively which invokes from index.html page...
As per the following code it is working(executing) in simulator & getting data from server during runtime. But when i am running in BlackBerry device only UI design is loading from index.html page but not getting the data from server. I am not getting any idea whats the error is.
I enabled javascript in Blackberry device also but still it is not working..
As i am new to this blackberry developing, unable to find whats there problem with my app while running in Blackberry device.
As per the following code should i have to add any external code to get data from server (like just to access internet in Blackberry device after signing)
In simulator it is working good & getting data from server. When executing in Blackberry device only main page UI (UI in index.html) is loading but not getting data from server.
Can anyone help me with this.....
This question is variation of questions "Why I have issues with connection on the BlackBerry". I'll try to give you idea how to solve them.
The network connection is much complex unlike j2me, android or iphone connection where you mostly ask system to open TCP/HTTP/UDP connection. RIM introduced Network Transports. I'm not sure about reasons - or they wanted to give developer power to select specific transport, or because BES admins/Carriers could restrict some connections, or other reasons. But side effect that BB developer has to specify which transport he wants to use for current connection on the device. The dramatic thing that default connection is Direct TCP (through the APN carrier settings). And direct TCP works perfect on simulators. But most used connection on the BB devices are BIS-B and WiFi. Also BIS-B was unavailable for usual developer before and they just opened it for everyone recently.
Before 5.0 OS developer had huge amount of code to determine what transports are available on the device and there was loop by transports trying to open in in order.
RIM introduced ConnectionFactory in 5.0 OS which provides standard mechanism to open determine available transports and open the connection (example).
You always have to use ConnectionFactory where you want to open connection. It doesn't matter if you reach remote host directly through Connector or through BrowserField. You have to use ConnectionFactory.
Dramatic thing is that even if you use it you can't reach BIS-B transport. As I mentioned before it was available only for alliance members. RIM doesn't restrict using of BIS-B for any developers right now. But I don't see that they also give any example. To allow ConnectionFactory to open BIS-B you need next line:
connFact.setTransportTypeOptions(TransportInfo.TRANSPORT_BIS_B,
new BisBOptions("mds-public"));
This is just small summary of connections on the BlackBerries. But it should give you right direction to solve your problem.

Monitor Data sent from Android app to Php server in Real time

I made an app that sends coordinates over to my php server. I am able to verify that this works because it returns the coordinates to my phone in a specific format that I set.
However, is there a way to monitor the coords in real time as the phone sends them over to the php server. For instance, can I open app/app.php in firefox and have the php code print the coordinates to my screen as I send them one by one?
I am using JSON to pass the data back and forth.
Thanks in advance
This answer seems slightly different than what you asked, but I think it could apply
I think a popular solution to this is to use Fiddler2
What you will do is use Fiddler to intercept traffic for sniffing or tampering, and set up your phone or app to use the PC running Fiddler as a proxy.
Configure fiddler to accept connections from remote devices, configure a proxy on your android phone or emulator to use the fiddler proxy, then it can sniff and modify all traffic send from the phone.
Fiddler help & tutorials
Tutorial: Getting Android Emulator Working With Fiddler
If you just want to be able to open a webpage on your server that displays coordinates in real time, some sort of ajax poller might be your best bet.
Create a webpage that uses Ajax to ask the server if any new coordinates have been sent since a given timestamp, have the server respond with the coordinates if any had been sent, append the results to a div in the browser. Keep polling for new coordinates as long as the browser page is open.

Push message to AIR app from Java Server

I am looking for solutions for a problem that we are facing. We are planning to make a POC for Desktop Alert application. We will have a desktop AIR app that will connect to a Java Server. The server will need to notify Desktop App when it has a notification which will be displayed on the screen.
A couple of options that we have discovered are :
1. Keep a persistent connection between the AIR app and the Server, using some sockets and listen to any changes.
2. Poll the Server at intervals to check if some new data has come.
Since these options don't seem optimal (persistent connection) or seems performance wise (polling) better.
Is there a better solution for this problem or do we have to choose one of these?
For this, you could use a BlazeDS message service if you want to push messages to your client. Or a regular remote service with polling enabled.
It is built in out of the box and well tested equipment :)
Other technologies like GraniteDS or WebOrb for java are also possibilities.
Cheers

Categories

Resources