Android Tutorial Custom WebSocket Based Push Framework

Discussion in 'Tutorials & Examples' started by Erel, Apr 24, 2014.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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.

    [​IMG]

    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.

    [​IMG]

    [​IMG]

    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.basic4ppc.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.basic4ppc.com/android/forum/threads/webapp-web-apps-overview.39811

    B4J client implementation: http://www.basic4ppc.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.
     

    Attached Files:

    Last edited: Jul 28, 2014
    MarkusR, jimmyF, luke2012 and 9 others like this.
  2. ofc500k

    ofc500k New Member Licensed User

    wait...
     
  3. Gabino A. de la Gala

    Gabino A. de la Gala Active Member Licensed User

    It works!!!

    Will be possible to send lists of things to do and then bookmark the different items as they are doing?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You can send any message you like with this solution. The client can also send messages to the server.
     
    DonManfred likes this.
  5. AscySoft

    AscySoft Active Member Licensed User

    It is possible to send message from one client to another?
     
  6. DonManfred

    DonManfred Expert Licensed User

    yes.You can raise an event on server to send a message to an other user if the server have such an event.

    I think we should wait the week till erel posts the server-code.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    A new version was uploaded. Please download the new project or APK and install it. The previous WebSocket url is no longer valid.
    There are several changes in this version. The device reports back to the server after receiving new messages. Only after this report messages are considered delivered. There is also a new heart-beat mechanism that closes the connection after 30 seconds of no server response. This is important in cases where the connection appears to be live but it is actually broken.

    The server code is now available: http://www.basic4ppc.com/android/forum/threads/webapp-web-apps-overview.39811
     
    luke2012 likes this.
  8. AscySoft

    AscySoft Active Member Licensed User

    thanks
     
  9. billzhan

    billzhan Active Member Licensed User

    Tested on two devices.
    It works on android 4.0 device (online and deployed on local network).

    But it doesn't work on another android 4.1 device (online and deployed on local network). It is the same after uninstall the antivirus software. The ws lib demo works fine on local network.
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    In which way doesn't it work?

    Have you tried it with this server?
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Both the server and the client code were updated. According to my tests the solution (with the updated code) should be highly reliable.
     
    lemonisdead likes this.
  12. billzhan

    billzhan Active Member Licensed User

    Erel, you are right.

    My Bad. In the debug mode,the PushService never start, it has been blocked by an app "root manager". After enable service to start from background, it works fine now.

    thanks,
    bz
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    The server code was updated. This update fixes an issue when multiple messages are added very quickly which caused the server to send the same message multiple times.

    Note that this update requires B4J v2.20+.
     
  14. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    How can I solve this problem
    upload_2014-5-22_17-23-5.png
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Which version of Basic4android are you using? It is a v3.80 project.
     
  16. YIM bunchhat

    YIM bunchhat Active Member Licensed User

    it is only 2.3
     
  17. Erel

    Erel Administrator Staff Member Licensed User

    The project will only work on B4A v3.80+
     
  18. EvgenyB4A

    EvgenyB4A Active Member Licensed User

  19. Erel

    Erel Administrator Staff Member Licensed User

    Don't you see a list such as:

    [​IMG]
     
  20. EvgenyB4A

    EvgenyB4A Active Member Licensed User

    I has updated the Google Chrome to latest version and now I can see all connections.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice