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:

DonManfred

Expert
Licensed User
Longtime User
If i read carefully he said that he added this to his project so i guess he has more than one activity in his project (due to his state that not always the main activity will be started.)

So i guess he is not using the new starter service of b4a. In fact he sould expect that android may start with a different activity. :)
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
If i read carefully he said that he added this to his project so i guess he has more than one activity in his project (due to his state that not always the main activity will be started.)

So i guess he is not using the new starter service of b4a. In fact he sould expect that android may start with a different activity. :)

It appears the activity but not functions are called and Activity_Create Activity_Resume. I put the log but do not display. And 'as if the activity is not able to handle events. does nothing.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
If i read carefully he said that he added this to his project so i guess he has more than one activity in his project (due to his state that not always the main activity will be started.)

So i guess he is not using the new starter service of b4a. In fact he sould expect that android may start with a different activity. :)
We have more than one activity, but I go by the main application, calling the ExitApplication
 

LucaMs

Expert
Licensed User
Longtime User
Difficile capire il problema, senza vedere il codice.
Prima di ExitApplication devi chiudere tutte le Activity e servizi. Secondo me, se l'app è chiusa definitivamente, non può mai ripartire da un'activity diversa dalla Main (a meno che non stai usando la nuova versione, con il servizio Starter).

It's hard to find the problem without see your code.
Before calling ExitApplication, you should close all the Activities and Services. I think that, if an an app is definitively closed it can not restart from other that Main (if you don't use the new version, with Starter Service).
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Difficile capire il problema, senza vedere il codice.
Prima di ExitApplication devi chiudere tutte le Activity e servizi. Secondo me, se l'app è chiusa definitivamente, non può mai ripartire da un'activity diversa dalla Main (a meno che non stai usando la nuova versione, con il servizio Starter).

It's hard to find the problem without see your code.
Before calling ExitApplication, you should close all the Activities and Services. I think that, if an an app is definitively closed it can not restart from other that Main (if you don't use the new version, with Starter Service).

Premetto che nella app che avevo già ho aggiunto il nuovo servizio Starter, come da esempio. Quindi, ho adattato il medesimo alle mie esigenze. Vengono chiuse tutte le Activity, ma quanto la app viene riaperta non vengono effettuate le chiamate alle funzioni. Se ho tempo, provo a ricostruire un esempio.

Premise that the app I had already added the new service Starter, as the example. So I adapted the same to my needs. They close all Activity, but as the app is opened are not carried out the function calls. If I have time, I try to reconstruct an example.

grazie
ciao
 

iCAB

Well-Known Member
Licensed User
Longtime User
I am using B4J server sample code as a base for a Push framework.
I did modify the code very little here and there.
I have over 100 tablets connected and things for the most part are going great.
For easier debugging ( as this is the first implementation ), I am running the code from the debugger and outputting all the communication to the Log window.
Once in a while all communication stops (meaning no more messages are being received from the tablets ), until I restart the program ( as soon as i do that all tablets connect back in 10-20 seconds max ).
I am just wondering what is the best way to capture the error that is causing this to happen,
Any ideas?? Would running the B4J with -log help capture anything in this case
 

iCAB

Well-Known Member
Licensed User
Longtime User
I was able to log the error using the suggested library.
I am hoping that someone can give me a suggestion.
if you open the log you will see that it all starts with: Error occurred on line: 157 (pushb4a).

After facing this issue several times, I added Try Catch to every single Sub trying to trap all errors and log the Sub name generating the exception.
As you can see it didn't work

Here is the code
B4X:
Public Sub Device_Ping(Data As Map)
Try
    lastPingTime = DateTime.Now
    CallSubDelayed(PushShared, "UpdateBrowsers")
    ws.RunFunction("Pong", Null)
 
Catch
    GeneralLib.GL_LogException(" Device_Ping: " & LastException)
End Try     

End Sub  'This is line 157


at the end of the log, I left 3 lines that say "java.lang.NullPointerException", however the log has several hundred lines that repeat the same exception

Thanks in advance
 

Attachments

  • B4J_ErrorLog.txt
    13.8 KB · Views: 369

iCAB

Well-Known Member
Licensed User
Longtime User
Unfortunately this is not the case.
Once this error occurs the only thing that I keep seeing in log is "java.lang.NullPointerException" and no communication takes place (from any of the clients ) until I restart the server program.
I am not sure how to deal with this error at the moment.
One thing I am sure of, is that the clients are continuously attempting to connect properly. The minute I restart the server, you will see them coming online quickly.
This is really urgent for me as it is causing major issues for us in the field.
If I can trap the error or detect the error properly, may be I can use a shell command or whatever to restart the program if necessary as a temp workaround.
Any help is greatly appreciated.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
For easier debugging ( as this is the first implementation ), I am running the code from the debugger and outputting all the communication to the Log window.

I am just wondering what is the best way to capture the error that is causing this to happen,
Any ideas?

I'm developing a b4j web server and I prefer to run it in release mode (for threads question).
I often use a useful tool, in these cases... my wonderful AddLogs :D
 

iCAB

Well-Known Member
Licensed User
Longtime User
Thanks for the advise LucaMs.
but as you can see in my code, I am doing something slightly similar (more or less ):

B4X:
GeneralLib.GL_LogException(" Device_Ping: " & LastException)

however somehow this particular exception is not being trapped properly using the added Catch block
 

LucaMs

Expert
Licensed User
Longtime User
I think your library can be very useful, but it is not similar to AddLogs.

AddLogs can help because using it you can see ALL routines called (with parameters) and you can know the routine that caused the error and the last command that worked well.

However, AddLogs were required little (I hope that the main reason is because in the beginning I had published it for free and many people have it :))
 

iCAB

Well-Known Member
Licensed User
Longtime User
Thanks for the advise, I will definitely consider it.. just for my info what is the usual Donate amount :)
 

iCAB

Well-Known Member
Licensed User
Longtime User
Have you modified the server code?
Yes, I did modify the server code.. so yes it is possible that my mods are triggering the issue, however I don't see how. Based on the captured log as it happened in the Device_ping, unless this was a our coincidence ( we will know better next time it happens ).

How long does it take until it happens?
I am not sure.. I believe sometimes it is more frequent than others... I am trying to keep an eye.. However if it is related to the message load, it can change depending on the activity taking place, which differs from day to day. ( Last it happened it was after 3 days from a reset, or at least I believe so )
 
Last edited:
Top