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

vfafou

Well-Known Member
Licensed User
Longtime User
Hi!
Does anyone have figured how to send messages from client to server? I've tried without luck! I think I'm missing something!
 

vfafou

Well-Known Member
Licensed User
Longtime User
It's all OK! I can now send messages from my Android phone.
Does web socket support simple socket connections?
I need to connect an old VB6 application to the B4J server in order to send/receive messages to/from Android tablets via internet?
Or I have to implement a simple socket server into B4J server?
Which is the best practice?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
No.

You will need to open another port on the server and listen to incoming connections with ServerSocket. A simpler solution will be to send regular http requests from your VB6 app and handle them with a regular server handler.

Thank you Erel!
I'll try to implement both solutions and see which is more suitable for my needs!
 

Levisvv

Member
Licensed User
Longtime User
does this library use only the cellular connection or will it also try the wifi connection?
I ask because I tried redirecting it to connect to a local TCP/IP server, but the android app report a timeout..
B4X:
Sub Process_Globals
    Public serverLink As String = "ws://192.168.1.121:7234"
End Sub

can anybody think of any reason why this never connected?
 

tigrot

Well-Known Member
Licensed User
Longtime User
Hi there,
I was wondering if the system would be limited by total number of connections per server or only by processing power. I'd like to use in a big network where a static connection whould be impossibile. I'm now using a poll, but I have long delays.
Regards
Mauro
 

Silv

Member
Licensed User
Longtime User
I have problem to connect to this serverLink ws://basic4ppc.com:51042/push/b4a_ws2 . Is anybody have the same problem?
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I'm trying to send message from my android client to server, running the following code
B4X:
Public Sub SendEventToServer(Event As String, Data As Map)
    Dim m As Map
    m.Initialize
    m.Put("type", "event")
    m.Put("event", Event)
    m.Put("params", Data)
    Dim jg As JSONGenerator
    jg.Initialize(m)
    ws.SendText(jg.ToString)
End Sub

Public Sub SendMessageToServer(mText As String, mId As String)
   
    Dim data As Map
   
    data.Initialize
    data.Put("id", mId)
    data.Put("message", mText)
   
    SendEventToServer("Device_Message", data)
   
End Sub
The server has the event Device_Message:
B4X:
Public Sub Device_Message(Data As Map)
   
    id      = Data.Get("id")
    mText = Data.Get("message")
   
    CallSubDelayed3(PushShared, "NewMessage", mText, Null)
   
    Log("Message From ID: " & id & ": " & mText)

End Sub

Server can send messages to device, via PushBrowser class.
What am I doing wrong?

Thank you in advance.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I took down this online example as it seems that someone has started "spamming" the system sending several hundreds of gibberish messages each day :(

Hi there,
I was wondering if the system would be limited by total number of connections per server or only by processing power. I'd like to use in a big network where a static connection whould be impossibile. I'm now using a poll, but I have long delays.
Regards
Mauro
Polling will require more resources. What is the estimated number of connections?
 

defillo

Member
Licensed User
Longtime User
Hello Erel

maybe I'm lazy in searching or simply something is not clear enough to me.. I have several domains hosted by an internet provider, all under linux servers.
Suppose I would like to use this servers for webapp push notifications.
what are the files I do have to upload and what is the link to connect to? tried several ways but with no result (I don't even know if webapp is allowed or not, but should be..)
 

tigrot

Well-Known Member
Licensed User
Longtime User
Note: there are toooo many idiots, everywhere.
Luca vai sicuro, che con un poll ogni 30sec. (keep-alive) il tuo server dopo i primi 10K clienti scoppia... Io l'ho installato su un server cloud di Aruba e va molto bene, ma ogni cliente chiede un socket e i socket sono limitati, a menu che ti riscrivi Linux come hanno fatto i vari network.
Puoi fare un test generando da un client 10K connessioni e guarda che succede. Mezz'ora di coding...

Ciao
Mauro
 
Top