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

vfafou

Well-Known Member
Licensed User
Longtime User
@Erel
I'm still testing the framework. Now I have connected ~80 android clients! The server works like a charm and it's very fast!
 

Erel

B4X founder
Staff 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!
1. You can use any database you like. You need to configure the connection pool correctly.
2. Start with the simpler solution which is to push data to all connected devices.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi Erel!
Thank you for your reply!
I'm currently pushing the data to connected devices and I have some lags. I need to implement a faster process of refreshing data!
I have 79 static records updated to web socket server database, every time I have changes to a table of my old VB6 application.
When an update occurs, I have to refresh the data to my clients. Now, I'm just sending 79 messages, one for each record...
Which is the best way to go?
 

vfafou

Well-Known Member
Licensed User
Longtime User
Of course, Erel:
B4X:
id = Data.Get("id")
mText = Data.Get("message")

If id <> "" Then
    Log("Message From ID: " & id & ": " & mText)
 
    Select Case mText
        Case "QZ"
            Dim c_zone As ResultSet
            c_zone = Main.ZoneDB.ExecQuery2("select zone_id,zone_st_count from gr_ath_zones_custom where zone_id < 80", Null)
         
            Do While c_zone.NextRow
                dZone  = c_zone.GetInt2(0)
                dQueue = c_zone.GetInt2(1)
                CallSubDelayed3(PushShared, "SendWebClientMessage", "ZR;" & dZone & ";" & dQueue, id)
            Loop
            c_zone.Close
      
       Case...

    End Select
End If

Where "SendWebClientMessage" is:
B4X:
Public Sub SendWebClientMessage(wText As String, wId As String)
    NewMessage(wText, Array As Object(wId))
End Sub
 

vfafou

Well-Known Member
Licensed User
Longtime User
I send those data after client request.
I send 79 messages after every request!
May the clients requesting the data will be more than one at the same moment.
The ideal situation is sending those data to all clients without request and without lags or delays!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Yes, with every request I'm sending those 79 messages to the user requested it. I know it's not a wise decision but I don't know which is the best technique to implement the refresh I want to do!
 

vfafou

Well-Known Member
Licensed User
Longtime User
By the way, from now on the Push Framework Server works with MariaDB 10.0.14.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi!
I would like to ask you if you have tried to implement live voice streaming via the framework.
Is it possible to have a real time voice broadcast to all connected users?
What about connections, internet lines and bandwidths?
Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi!
Still testing the framework!
~250 tablets and running perfectly fast!
I'll give the final release of my app within a few days.
I'm sure that the framework will work as fast as now with 300, 400, 600 tablets!
I'll install all my infrastructure to a new dedicated server machine, because it is running on a Mac Mini 2008 now!
Thanks to B4A for this Rapid tool!
Thank you Erel :):):)
 
Top