rmh
rmh

Reputation: 179

Socket.io Android HTTPS - Connection Error xhr poll error

I'm developing a chat application with HTTPS. This is the Socket.IO I've used

Server

First of all I developed the server with node.js and node module "socket.io": "^1.4.5"

In the server I have this:

var secure = {
  ca: fs.readFileSync('ca.pem'),
  key: fs.readFileSync('key.key'),
  cert: fs.readFileSync('cert.crt')
};

var server = https.createServer(secure, app);
var io = require('socket.io')(server);

Clients

I developed the website with https://cdn.socket.io/socket.io-1.4.5.js

I developed the iOS application with pod 'Socket.IO-Client-Swift', '~> 5.5.0'

Finally I'm developing the Android application with

compile ('io.socket:socket.io-client:0.7.0') {
  exclude group: 'org.json', module: 'json'
}

In the website and in iOS application I haven't problems with the websocket, but when I try to connect the socket in Android application I receive the following error:

io.socket.engineio.client.EngineIOException: xhr poll error

Thanks

Upvotes: 5

Views: 5987

Answers (3)

Thijs
Thijs

Reputation: 738

If you're trying to connect through HTTPS and use Nginx as a reverse proxy on the backend, make sure to configure the upgrade options correctly:

upstream nodes {
  # enable sticky session based on IP
  ip_hash;
  # Backend nodejs server
  server 127.0.0.1:5500;
}


server {
  listen 80;
  # The host name to respond to
  server_name mysite.com;


  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;


    proxy_pass http://nodes;


    # enable WebSockets
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
} 

This worked for me in solving this mystic error.

Source: https://nhancv.com/android-socket-io-client-v1-0-0-engineioexception-xhr-poll-error/

Upvotes: 1

Rahul Kathet
Rahul Kathet

Reputation: 135

I hope this resolve your error:

check your authentication header you are sending from browser, iOS and android. Could because of authentication issue.

Using Socket.IO on android Always Returns XHR Poll Error

https://developer.android.com/training/articles/security-ssl

Upvotes: -1

beplaya
beplaya

Reputation: 281

This most likely a problem with your installed android version using an ssl socket protocol that your server won't accept or vice versa.

I believe this the case, because I have the same problem with Android 4.4.4 but my 5.1.1 tablet works fine.

Another instance we've seen this problem with with our OkHttpClient against a spring server where we had to extend our own DelegateSSLSocket that implements:

    @Override
    public void setEnabledProtocols(String[] protocols) {
        super.setEnabledProtocols(new String[]{TlsVersion.TLS_1_1.javaName()});
    }

So I THINK this is a very similar issue. Anyway, a work around is to update your Android OS to 5.1.1. If you find another way without updated the Android OS please let me know!

Upvotes: 0

Related Questions