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

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
Is there any way to search and find memory leaks?
How can I force release - dispose - destroy of a thread?
Is it possible runtime errors to leave bad data in memory?
Could Try-Catch-End Try block help to avoid such situations?
Thank you in advance!
 
Last edited:

Reids

Member
Licensed User
Longtime User
Hello how I can start websocket connection when activity is closed = true (apps is killed)
I can call the service when app sleep to call connection sub

B4X:
If IsPaused(Main) = True Then
        Log("apps killed")
        Connect
    End If


B4X:
Private Sub Connect
    Try
        Dim wsh As WebSocketHandler
        ws.Initialize("ws")
        ws.Connect(Main.serverLink)
    Catch
    End Try
End Sub


but websocket connection could not be made, no connection is made it keep throwing me to reconnect

B4X:
Sub ReconnectTimer_Tick
    Try

        If ws.Connected Then
            Log("websocket still connected")
        Else
            Log("websocket is dead so reconnect")
            Connect
        End If
        'ReconnectTimer.Enabled = False

    Catch
    End Try
End Sub
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
Where do I find the latest:
1. sample code client (B4A)
2. server code
3. latest client library

Thanks
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I want to inform you that after my latest configurations to my Database Server, the framework rocks!
I have ~650 concurrent tablets connected to the web server and the response is rapid!
Thanks Erel for all his help!

I think that it's time to secure the web server.
I mean that I want to add an authentication process.
Which is the best practice for securing a web sockets server?

Thank you in advance!
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel
I downloaded the following Sample projects:
1. WebSocketExample (B4A) & Server B4A-WebSocket (B4J)... Things are working no problem

For example: Say I have 2 clients:
1. Client "A"
2. Client 'B"
when the clients sends a message, it adds an identifier in the message (Client_X ).
Say the main program is reading a log of messages: each message is addressed to a specific Client. For example:
Client_A: Send A
Client_B: Sent B

Can you please show me the sample code that can be used by the main program to send the message to the proper client

Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Which is the best practice for securing a web sockets server?
WebSocketClient currently doesn't support wss connections.

Can you please show me the sample code that can be used by the main program to send the message to the proper client
You will need to go over the code and learn it. NewMessage from PushShared expects two parameters. The message text and an array of client ids. You need to call this method with CallSubDelayed with the message and specific client id.
 

iCAB

Well-Known Member
Licensed User
Longtime User
Hi Erel

May be we are not talking about the same code. Let me clarify something please.
In B4A-WebSocket Example, I see this code in the timer
B4X:
Sub Timer1_Tick
    ws.RunFunction("ServerTime", Array As Object(DateTime.Time(DateTime.Now)))
    ws.Flush
End Sub

The code above references the instance for the socket to send data.
I was wondering how can I reference or identify the instance referred to in this code from the main program.

Thanks again
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi iCAB,
See the following modules (hope this helps!):

From PushB4A
B4X:
Public Sub SendMessages(Messages As List)
    ws.RunFunction("NewMessage", Messages)
    ws.Flush
End Sub

From PushShared
B4X:
Private Sub SendUserMessages(id As String, pb As PushB4A)
    PSQL = Main.Pool.GetConnection
   
    Dim table As List = DBUtils.ExecuteMemoryTable(PSQL, _
        "SELECT messages_d.id_msg, message FROM messages_q, messages_d WHERE messages_q.id_msg = messages_d.id_msg AND id_device = ?", _
            Array As String(id), 0)
   
    PSQL.Close
   
    If table.Size > 0 Then
   
        Dim msgs As List
       
        msgs.Initialize
       
        For Each row() As String In table
            msgs.Add(row(0))
            msgs.Add(row(1))
        Next
       
        CallSubDelayed2(pb, "SendMessages", msgs)
   
    End If
   
End Sub
B4X:
Private Sub SendSingleMessage(MsgId As String, Msg As String, pb As PushB4A)
    Dim msgs As List = Array (MsgId, Msg)

    CallSubDelayed2(pb, "SendMessages", msgs)

End Sub
 

vfafou

Well-Known Member
Licensed User
Longtime User
WebSocketClient currently doesn't support wss connections.
Is it possible to support wss in the near future?
What else I could do to secure my web server?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
OK, I've got it!
But how to protect it from possible hacker attacks?
There is a port open and available to the web!
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello to all
I entered your framework in my app, sometimes when I close the app and relaunch is not called the main activity. This occurs because there is no connection to the server and made the call to connect. I tried to insert DoEvents, but does not solve the problem.
Can you tell me if there is another way to solve the problem.
thank you
 
Top