Wish WebSocket Handler B4X

Discussion in 'Bugs & wishlist' started by Jmu5667, Aug 20, 2018.

  1. Jmu5667

    Jmu5667 Well-Known Member Licensed User


    It would be great to have a built in PING function for the explicit purpose of keeping the data connection open. This would really apply on android 8.0+ when a service has a data connection open but you want to minimize the amount data to send over the connection.

    We have successfully built a PTT server(B4J) + client(B4A), and are now trying to optimize the bandwidth used for the WeSocket.

    Hope you consider this :)
  2. Erel

    Erel Administrator Staff Member Licensed User

    I don't think that there will be any difference between sending a ping message or sending a small text message. The length of the data sent will be the same.
  3. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    Ok, thanks for the reply and consideration. currently i have it a 50 bytes, {"type":"event","event":"c_p","params":{"X":"X"}} (this is fromt he send event to server)
  4. OliverA

    OliverA Expert Licensed User

    Looks like you have 10 bytes of framing per packet after a WebSocket has been initiated (see https://stackoverflow.com/a/12739418). If you think 50 bytes is too much per ping, then (if you're in control of both the client and server protocols) you could create maybe something like this
    which would bring you to 15 bytes and be a type that is just ignored by the server.
    Other solution(?): Include a session parameter. This way if the connection is broken and the server sees a new WebSocket connection, it can first see if this was a previously active session and process the information accordingly. Then you would not need a ping/keep-alive mechanism. This may also cover situations where the network connection was disrupted.
    Update: If it is a high traffic connection with pauses now and then, I would go with the ping route. If this is a low traffic connection with pauses then I would look into implementing sessions. The session information overhead may be worth it for the high traffic connections if network interruptions are common and or can wreak havoc with your communication process.
    Jmu5667 likes this.
  5. Jmu5667

    Jmu5667 Well-Known Member Licensed User

    Hi Oliver

    I am using the websocket (b4j) and the websockethandler class (b4a) so as far I am aware you can just sent text, I am using :

    Sub send_heartbeat
    Dim data As Map
    End Sub
    This is ok as I do need to clock the time the ping occurred so I can manage timeouts from the client. It is a PTT system so the server has websockets for handling mesaging etc and UDP for the audio.


  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