socket.io xhr post error on slow connection (3G mobile network)

2.6k views Asked by At

While I am testing my real chat application on 3G mobile network (Slow internet connection), Socket.io repeatedly disconnects then reconnects. I have logged the reason. it says "xhr post error" which raise "transport error" then disconnect.

May I know what is the meaning of "xhr post error" and why this error appears in slow connection and how to solve the problem?.

I am using socket.io-client.java version 0.4.2

2

There are 2 answers

0
amlwin On

First of all update the dependency to 1.0.0

implementation ('io.socket:socket.io-client:1.0.0') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
 }

And create the option with transport

val ioOption = IO.Options().apply {
            query = /*..*/
            reconnection = /*..*/
            transports = arrayOf("websocket")
        }

And then create socket connection with that option

try {
      socket = IO.socket(baseUrl, ioOption)
     } catch (e: URISyntaxException) {
       e.printStackTrace()
     }

 socket.connect()

and listen on EVENT_RECONNECT_ATTEMPT

socket.on(Socket.EVENT_RECONNECT_ATTEMPT) {
            ioOption.transports = arrayOf("polling", "websocket")
}

there is no magic under the hood just implement what document told . Read More

NOTE: you can change appropriate transport name, here I used websocket to be same as document.

1
Muhammad Reda On

I think you should set transports at your client to use websockets.

Try this:

IO.Options opts = new IO.Options();
opts.transports = new String[]{"websocket"};
mSocket = IO.socket(SOCKET_SERVER, opts);

Update Also, try to update your client library to 5.1+.