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.basic4ppc.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.basic4ppc.com/android/forum/threads/webapp-web-apps-overview.39811

B4J client implementation: http://www.basic4ppc.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: 3,747
  • WebSocketPush.apk
    143.7 KB · Views: 2,309
Last edited:

DonManfred

Expert
Licensed User
I really should understand the system....

The external message does not reactivate a connection!
The incoming message wake up (or starts) your app. Once your app is started it is up to YOU then to do whatever you need to do to sync your database or whatever you(your app) wants to do...
 

LucaMs

Expert
Licensed User
You can also use StartServiceAt to start the process at a specific time.
Maybe I'm lost somewhere (missing the meaning of some post) but I'm reading the PushService service and it works exactly using StartServiceAt.

1) PushService starts at boot
2) it is a sticky service
3) it uses StarServiceAt (every 2 minutes)
4) it tries to connect to the server, if the connection is not already active
5) it reads and shows messages stored locally (if exist)
6) it can receive new messages from the server, without starting some "listening method", thanks to the websocket (the server "runs" wsh_NewMessage)

So, I don't understand Roberto's question...
For further discussion please start a new thread.
... please, start a new thread also in the Italian forum ;)
 
Last edited:

tigrot

Well-Known Member
Licensed User
If you don't need a real time response, yes. I close connection and every now and then i get a web page to see if the server has data waiting for me. Also "institutional" push notification services are unable to contact the client if the client is not connected at that time. And connection must be handled automatically or manually, since TCP is not able to reveal connecction's drops real time.
 
Top