Wish WebSocket Handler B4X

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hello

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 :)
 

Jmu5667

Well-Known Member
Licensed User
Longtime 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.

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)
 

OliverA

Expert
Licensed User
Longtime User
currently i have it a 50 bytes, {"type":"event","event":"c_p","params":{"X":"X"}} (this is fromt he send event to server)
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
B4X:
{"type":"ping"}
which would bring you to 15 bytes and be a type that is just ignored by the server.
or
B4X:
{"ping"}

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.
B4X:
{"type":"event","event":"c_p","params":{"X":"X"},"session":"SomeSortOfSessionID"}

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

Well-Known Member
Licensed User
Longtime 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
B4X:
{"type":"ping"}
which would bring you to 15 bytes and be a type that is just ignored by the server.
or
B4X:
{"ping"}

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.
B4X:
{"type":"event","event":"c_p","params":{"X":"X"},"session":"SomeSortOfSessionID"}

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.

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 :

B4X:
Sub send_heartbeat
 
   Dim data As Map
 
   data.Initialize
   data.Put("X", "X")
   wsh.SendEventToServer("c_p",data)
 
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.

Regards

John.
 
Top