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,528
  • WebSocketPush.apk
    143.7 KB · Views: 2,924
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
Honestly, I don't see any issue related to the load. In fact I modified the code to work as a message relay without logging any messages to the database. I believe the server code should be able to take 10 times (if not more ) the load that we are putting on it easily.
My concern, is that one of the underlying components could be failing somehow.
What puzzles me is the fact that it was triggered in the Ping event but it wasn't caught by the catch block.
Please provide me as much info as possible, so I can get to the bottom of it before losing the client
 

iCAB

Well-Known Member
Licensed User
Longtime User
I would try the original example, this would exclude configuration problems (if it worked, of course).

Honestly I do see your point, however at this stage it will be close to impossible to do that, as the B4J server is acting as a middleware between the units in the field and another host on the local network.
All what I am hoping for at this stage is to catch the error using, Try catch. If I can do that, I would at least be able to identify it and possibly use some code to restart or something along these lines until we know the exact cause.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Does anyone know if it's a good idea to move the code of the PushService to Starter service module?

Thank you in advance!
 

IslandMedic

Member
Licensed User
Longtime User
If you need MySql it's better to write a WEBSERVICE, so you don't have to care about connections(and you DB passwords are not published!) I have written some in PHP and vb.net. If you need them send only have to ask!
I would love to review your Web service code to access mysql. Trying to figure out the best way...

Thanks

Brad
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I've noticed that if the internet connection is lost, then the server side does not reconnect to the internet.
Is there any action I have to take, in order to reconnect the server app to the internet?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
I've finally found that all dysfunctions are caused by the database (MariaDB 10.1). On Device_Ping, I update every 10 seconds the table with the positions of the devices. So, if I have 300 concurrent updates then the table locks are much more and the application is running very slowly!
I have to find a better solution than storing locations to a DB table.
What is the best practice in order to find the closest to a point devices?
Storing coordinates to a map/list?
If yes, is there any fast and reliable way to find from map/list the closest devices?
Thank you in advance!
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
How reliable is this solution in providing real time push messages ?

Can it be a perfect alternative to GCM ?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Can it be a perfect alternative to GCM ?
It depends on your requirements. For the custom push framework to work properly the process must be running, unlike GCM which is handled by the OS and only when a new message arrives it wakes up the process.

This solution is less suitable for a public consumer app.
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
It depends on your requirements. For the custom push framework to work properly the process must be running, unlike GCM which is handled by the OS and only when a new message arrives it wakes up the process.

This solution is less suitable for a public consumer app.
Is it possible to develop it so it can work like GCM ?
I think some companies have developed their own push messages .

Can we for example have some internet intent that wakes service when push is sent ?

Thanks in advance
 

vfafou

Well-Known Member
Licensed User
Longtime User
Is it possible to develop it so it can work like GCM ?
I think some companies have developed their own push messages .

Can we for example have some internet intent that wakes service when push is sent ?

Thanks in advance

Hello, somed3v3loper.
I would like to ask you what kind of application do you want to make?
I have made a taxi fleet management application, with ~450-500 concurrent tablets connected, on an old HP proliant G6 1U server with 4core XEON CPU and 16GB RAM with MariaDB 10.1 database. After optimizing the database instance, whole system is working perfectly.
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
No. This is technically not possible.

The only way to send push messages to Android devices without running a process all the time is with GCM.

But parse.com for example has a push messages service how did they create it ?
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Hello, somed3v3loper.
I would like to ask you what kind of application do you want to make?
I have made a taxi fleet management application, with ~450-500 concurrent tablets connected, on an old HP proliant G6 1U server with 4core XEON CPU and 16GB RAM with MariaDB 10.1 database. After optimizing the database instance, whole system is working perfectly.

Hi vfafou,

I am thinking of developing real time chat.
 
Top