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

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I would like to ask you if it is possible to make a class at the server application (B4J), running in its own thread and do the following work:
Fetch from the database some records and send to the clients (tablets) some information.
Having a timer (or is it any other and better way???), looking for changes to these records and resend to the clients the new information.

Thank you in advance!
 

Jerez

Active Member
Licensed User
Longtime User
I've a little problem. I've implemented this awesome piece of code and it work at home. But when I run this solution in another "internet provider" (my office) does not work!!

Can the internet provider or router lock push messages? I've solved the problem adding a router B to the router A (my internet provider).
At this time I can't sell my app with this problem. Any tip or solution?

I'm using the port 51042 "ws://myipaddress:51042/push/myservice" could be the problem?

Use port 80 cloud be possible?
 
Last edited:

Jerez

Active Member
Licensed User
Longtime User
You can use any port you like.

Is the server firewall configured to allow incoming connections on the relevant port?

Hi Erel:

The B4J App is running in a Raspberry Pi board. The board is connected with RJ45 to a router from the internet provider. And the Android phone need to be connected outside home with the public ip of the Raspberry pi.

The Android phone has the raspberry pi public ip and port.

What can I do if the router (provider) block all ports and just have open the standard ports such as 80, 21, 443, etc?

Think in a client that don't know what is a "firewall" and don't have access to the router settings. What can I do in this case?

You say:You can use any port you like.

I've tried the port 80 but Error:

B4X:
srvr.Initialize("srvr")
    srvr.Port = 80  '51042
  
    PushShared.Init
    srvr.AddWebSocket("/push/myservice", "PushB4A")  
    srvr.DebugNetworkLatency = 200
    srvr.Start

java.net.BindException: Address already in use: bind
 
Last edited:

Jerez

Active Member
Licensed User
Longtime User
Please start a new thread for this in the B4J forum.

Sorry Erel. but you make this thread based in an example framework with B4J server and B4A client code. My question is not about code! just setup! The solution could help to others in same problem. so?
 

cbal03

Member
Licensed User
Longtime User
Exactly. For it to be helpful to other developers who deploy B4J servers it should be posted as a new thread in B4J forum.
was a new thread started for this question?
I've a little problem. I've implemented this awesome piece of code and it work at home. But when I run this solution in another "internet provider" (my office) does not work!!

Can the internet provider or router lock push messages? I've solved the problem adding a router B to the router A (my internet provider).
At this time I can't sell my app with this problem. Any tip or solution?

I'm using the port 51042 "ws://myipaddress:51042/push/myservice" could be the problem?

Use port 80 cloud be possible?
Were you able to solve this problem? I couldn't find thread for this problem.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hi to all
I made a simple solution (client / server) test. I found that when the device is inactive for some time, it lose the connection and bring no more messages.
There is a way, as other applications (email, WhatsApp) to force sending messages?

thank you
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Hello (ciao) Tigrot
there is the risk of creating too much traffic to the server (if you have many apps) and consume too much battery of the device?

Possible that there is another way to keep alive the connection and allow you to send push messages to the app when there is an event?

thank you

p.s. piacere di conosceti...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is a way, as other applications (email, WhatsApp) to force sending messages?
Yes. Use GCM or FCM (firebase push notifications service).

there is the risk of creating too much traffic to the server (if you have many apps) and consume too much battery of the device?
It will not generate too much traffic. Hard to say about the battery. However if you are looking for a simple push framework then you should use Firebase Notifications.
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Yes. Use GCM or FCM (firebase push notifications service).
It will not generate too much traffic. Hard to say about the battery. However if you are looking for a simple push framework then you should use Firebase Notifications.

ok, I'll try this solution, even if it costs me a job rewriting code.

I take this opportunity to ask you if this system also works to launch the procedures for exchanging data automatically between the app and the server?
Thank you
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
Your app will start when a push messages is received. You can do whatever you like at that point.

Erel
thanks you for the answer, but I do not understand why an external message can reactivate the connection and not the example timer?
Possible that there is no other way to start the messages with the service timer?
 
Top