Android Tutorial Custom WebSocket Based Push Framework

The online example is currently disabled due to spam :(

Now that both B4A and B4J support WebSockets it is possible to implement a full push framework solution.
The solution is made of two components. The client app (B4A) and the server WebApp (B4J).

The client opens and maintains a WebSocket connection with the server.

SS-2014-04-24_17.31.41.png


In the browser you can see the number of active connections and the total number of users (which includes inactive users). You can send a message to all users.

The message is queued in a database and will be delivered to the devices when they connect.

A similar process happens on the device. If the activity is paused then the message is stored with the help of KeyValueStore and a notification is shown. Later when the activity becomes visible the messages are listed on the device.

SS-2014-04-24_17.35.08.png


SS-2014-04-24_17.35.29.png


Note that the device can also send messages to the server (it is not implemented in the demo interface).
It is also possible to send messages to specific ids.

Please give it a try. You need to first download WebSocket library (v1.01+): http://www.b4x.com/android/forum/threads/40221/#content
Run the program and then go to the online console to send a message: http://basic4ppc.com:51042/push/index.html

You can also download the compiled apk and install it.

The server code is available here: http://www.b4x.com/android/forum/threads/webapp-web-apps-overview.39811

B4J client implementation: http://www.b4x.com/android/forum/threads/jwebsocketclient-library.40985/

Edit: Both the server code and device code were updated.
This example can be the base for many types of solutions that require a persistent server connection.
 

Attachments

  • Push_Client.zip
    11.5 KB · Views: 4,531
  • WebSocketPush.apk
    143.7 KB · Views: 2,926
Last edited:

Phunny

New Member
Licensed User
Longtime User
Don't confuse websockets with this specific example that is indeed running all the time.

You can use websockets in any application and only keep the connection open when the app is active.


thats i mentioned : allways-on application on portable devices which mostly stay in background better not use websocket.

means an app which getting notified about activity and process the messages even when in background

like a newsticker, or even a chat like whatsapp ...
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello! I'm testing the framework with 10 max tablets connected for approximately 2 weeks! I've noticed that every ~2 days of current activity, web connections drop and the error I take is:

java.lang.RuntimeException: java.lang.RuntimeException: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSING], expecting [OPEN or CONNECTED]

During working state, the framework has excellent behavior.

Does anyone know what is the problem?
The web server is running on a Mac Mini year 2008, OSX Lion with Java version 1.7u65.

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
Thank you for your reply!
After this error, the server is still online.
I can hit its IP from browser and see the last messages without any information about connected devices and total number of users, but I can't hit it from the Android application.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Yes, it is running in Release Mode.
I don't know if this problem is MacOS related. Is it possible?
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
No, this is not the full error message. The full message is the following:

Connection has been closed locally
java.lang.RuntimeException: java.lang.RuntimeException: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSING], expecting [OPEN or CONNECTED]
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:114)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4j.object.WebSocketModule$Adapter$ThreadHandler.run(WebSocketModule.java:195)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSING], expecting [OPEN or CONNECTED]
at anywheresoftware.b4j.object.WebSocket.set(WebSocket.java:142)
at anywheresoftware.b4j.object.WebSocket.set(WebSocket.java:132)
at anywheresoftware.b4j.object.WebSocket.RunFunction(WebSocket.java:298)
at rm.rmtunnel.pushb4a._device_ping(pushb4a.java:113)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
... 10 more
Caused by: org.eclipse.jetty.websocket.api.WebSocketException: RemoteEndpoint unavailable, current state [CLOSING], expecting [OPEN or CONNECTED]
at org.eclipse.jetty.websocket.common.WebSocketSession.getRemote(WebSocketSession.java:245)
at anywheresoftware.b4j.object.WebSocket.sendText(WebSocket.java:107)
at anywheresoftware.b4j.object.WebSocket.set(WebSocket.java:140)
... 17 more
 

vfafou

Well-Known Member
Licensed User
Longtime User
I think that something else causes the connection dropping as well as you.
This is the reason I've asked you if it is possible to cause it something that is MacOS related.
The server really continue to work fine! I still can see the web page from the browser!
Is it right to suppose that your online example is running under Windows/Windows Server?
If yes, which is your Windows version?
Have you configured something specific at the OS?
In fact I'm planning to run it under Windows.
The Mac Mini was immediately available for testing.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
That's good news!
I love Linux! I will try it to see the results!
Thank you for the info... :);)
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello tigrot!
I'm in testing mode yet!
I think if it hadn't have this problem, would be perfect for my needs!
It has rapid response and it's working very smoothly, even if the devices' connections are mobile!
I will test it for some weeks on a Linux server and I'll inform you about the results.
I have still in mind your suggestion! :)
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
After 2 weeks of testing, I have to tell you that the framework is running perfectly good under Ubuntu installed on the same Mac Mini!
Sometimes I see these messages, but it doesn't drop all connections! It's up and running!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel,
I have done some changes in order to give to an older application written in VB6, the capability of the communication with multiple tablet devices via mobile internet connections. It's a telematics app working in parallel with digital UHF transceivers for transferring data too.
The estimated number of tablets will be finally about 500-600.
I'm still on testing!
 

incendio

Well-Known Member
Licensed User
Longtime User
Just for info, i used RDC to send message between server and clients or clients to clients and to do some others tasks.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I would like to ask you 2 questions:

1. Is it possible to use a MariaDB database on the server side?

2. Which is the best way to have fast refreshes of server data to my android web clients? Sending maps of data after request from android client or remote database connection from android client to push framework server?

Thank you in advance!
 
Top