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

LucaMs

Expert
Licensed User
Longtime User
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


Ecco, quindi è necessario usare i WebSocket, così si evita il "polline" :)

Grazie per l'informazione



So, I need to use WebSockets, to avoid the polling and the.... explosion of the server with just ;) 10K connections.

Thanks for the info.
 

tigrot

Well-Known Member
Licensed User
Longtime User
WebSocket in realtà utilizza un polling invisibile, quindi attento ai volumi e prima di vendere la pelle dell'orso prova!

Websocket is using an invisible polling to realize a Keep-alive function, so be careful with bandwidth and before selling your product.
 

picenainformatica

Active Member
Licensed User
Longtime User
Websocket open a bidirectional channel (one connection for each client). Standard web polling (from client) open, close and reuse connections.
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hi! I have tested some days all this framework and I can say that it works very well. The only problem I have seen, is that Android takes a long time to send web socket disconnection, but I want to disconnect as immediately as possible!!! Actually, I'm working on a login solution that uses the PushB4A.Id as Username. When the user gives a wrong Username, I have to disconnect and reconnect in order to give connection a new Id, same as the new Username.
I've noticed that when the intent of the Android client is terminated, then disconnection is immediately done!
Thank you in advance.
 
Last edited:

vfafou

Well-Known Member
Licensed User
Longtime User
Send a message to the device that it needs to disconnect instead of disconnecting the websocket immediately.

Hello Erel!
Thank you for your reply. This message will be sent from the server to the device or the device will send a message to tell the server that needs to disconnect?

BTW, I have forgotten to ask you: Is it required to use MySQL instead of SQLite on the server side? The traffic is not so big! I send and receive very small messages. For example: 134,OK,<LONGITUDE>,<LATITUDE>.
I don't know what will be if I connect 300-350 devices.
What do you think?
 

tigrot

Well-Known 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!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello tigrot!
Thank you for your reply! I would like to try this web socket push framework and if it is not OK with all these android devices, then I will ask you for help with the web service!
Thank you for your kind suggestion too!
 

coslad

Well-Known Member
Licensed User
Longtime User
hi, i need to use an android devices as server, so it would be nice to write the server with b4a and not with b4j. is it possible?

-----------------

per lucams e tigrot : w l' italia
 

coslad

Well-Known Member
Licensed User
Longtime User
I don't understand why you advise me a HttpServer . I don't know anything about http but i think that it can be easy build a connection with tcp ip socket .
I think that creating an http server includes writing a php page.
Is there any particular reason why I can not create messages that travel through socket tcp ip?

Otherwise have you any good tutorial to study, so i can begin to understand you when you say : "You can use HttpServer to implement an Android server" ?

@ Tigrot

Scusami ma scrivevo dal cellulare , ovviamente "Viva l'Italia"
 

Phunny

New Member
Licensed User
Longtime User
Websocket is cool
nice to made is happen in b4j and b4a
but .. i see not only the data volume but the batterie drain
websocket sends every 2 seconds a keepalive back and forward
i can only imagine the impact of battery drain

i tested with email poll every 30 min all other services stopped
connect locked to hspa only battery was on 20% after 6h
with email like first test and websocket demo which only sends the check back and forward without data passed the cell switched of after 3h because of reaching 1%

both test started with the same battery allways charged to 100%
email was using imap with poll every 30 min without any change of the amount of email.

conclusion: allways-on application on portable devices which mostly stay in background better not use websocket.
at least unless we are able to change the keepalive check params to more relaxed timing.

my 5 cent's
 
Last edited:
Top