New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XHR Polling Error appears when debugger is present #1097

Open
waseemshahwan opened this Issue Apr 13, 2017 · 17 comments

Comments

Projects
None yet
@waseemshahwan

waseemshahwan commented Apr 13, 2017

You want to:

  • report a bug
  • request a feature

Current behavior

I am experiencing trouble trying to make a web socket connection on react-native. I am not sure if this platform is supported or not, however, this error is reproducible on a regular node.js app. Strangely enough I only have trouble when react-native's built in debugger is disabled. I am testing this on IOS 10.2 with react-native v0.43.1 and socket.io-client v1.7.3. My server is running socket.io v1.7.2 hosted on HTTP port 1337.

Steps to reproduce (if the current behaviour is a bug)

As soon as I implement socket.io-client in react-native and attempt a connection without the debugger enabled, it should reproduce with two errors based on the options. If I do not specify my transports option then the error would be XHR: POLLING ERROR. If I specify 'websocket' in my transports option then the error would become WEBSOCKET ERROR. Thats all I get.

Expected behavior

Regular web socket connection to my server. It works with the debugger enabled.

Setup

  • OS: OSX El Capitan 10.11.6
  • browser: I believe react-native uses webkit, however i am not sure.
  • socket.io version: stated above

Other information (e.g. stacktraces, related issues, suggestions how to fix)

The only other issue I found related to this one was #1056 and I am surprised about how oddly related these issues are. I have retried the scenario on a regular node.js app and I experienced similar outcomes, however the errors were more detailed as shown below.

Without a specified transports option:

{ Error: xhr poll error
    at XHR.Transport.onError (/path/to/root/node_modules/engine.io-client/lib/transport.js:64:13)
    at Request.<anonymous> (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:129:10)
    at Request.Emitter.emit (/path/to/root/node_modules/component-emitter/index.js:133:20)
    at Request.onError (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:307:8)
    at Timeout._onTimeout (/path/to/root/node_modules/engine.io-client/lib/transports/polling-xhr.js:254:18)
    at ontimeout (timers.js:365:14)
    at tryOnTimeout (timers.js:237:5)
    at Timer.listOnTimeout (timers.js:207:5) type: 'TransportError', description: 503 }

With transports: ['websocket']:

{ Error: websocket error
    at WS.Transport.onError (/path/to/root/node_modules/engine.io-client/lib/transport.js:64:13)
    at WebSocket.ws.onerror (/path/to/root/node_modules/engine.io-client/lib/transports/websocket.js:149:10)
    at WebSocket.onError (/path/to/root/node_modules/ws/lib/WebSocket.js:452:14)
    at emitOne (events.js:96:13)
    at WebSocket.emit (events.js:189:7)
    at ClientRequest.onerror (/path/to/root/node_modules/ws/lib/WebSocket.js:711:10)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:189:7)
    at TLSSocket.socketErrorListener (_http_client.js:358:9)
    at emitOne (events.js:96:13)
  type: 'TransportError',
  description: 
   { Error: socket hang up
       at TLSSocket.onHangUp (_tls_wrap.js:1117:19)
       at Object.onceWrapper (events.js:291:19)
       at emitNone (events.js:91:20)
       at TLSSocket.emit (events.js:186:7)
       at endReadableNT (_stream_readable.js:974:12)
       at _combinedTickCallback (internal/process/next_tick.js:74:11)
       at process._tickCallback (internal/process/next_tick.js:98:9)
     code: 'ECONNRESET',
     type: 'error',
     target: 
      WebSocket {
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        _socket: null,
        _ultron: null,
        _closeReceived: false,
        bytesReceived: 0,
        readyState: 0,
        supports: [Object],
        extensions: {},
        _binaryType: 'nodebuffer',
        _isServer: false,
        url: 'wss://SERVER_IP:1337/socket.io/?EIO=3&transport=websocket',
        protocolVersion: 13 } } }

Also keep in mind, socket.io's browser-based client works flawlessly connecting to this specific web server.

I have tried various different networks, ISP's, and even VPN's. I am clueless at this point.

@waseemshahwan

This comment has been minimized.

Show comment
Hide comment
@waseemshahwan

waseemshahwan Apr 15, 2017

UPDATE: After finally testing one of my applications on my iPhone with socket.io-client, I have been fortunate not to come across the XHR Polling error. I am still, however, experiencing this error on a regular node app.

waseemshahwan commented Apr 15, 2017

UPDATE: After finally testing one of my applications on my iPhone with socket.io-client, I have been fortunate not to come across the XHR Polling error. I am still, however, experiencing this error on a regular node app.

@ibrahimduran

This comment has been minimized.

Show comment
Hide comment
@ibrahimduran

ibrahimduran Apr 16, 2017

I'm currently having the same issue with my node application too. I use built-in server of socket.io and it seems to work fine in browser but in node app socket.io-client throws the same 503 xhr poll error above. I'm running server and client on the same machine but connecting to loopback or local network address from client application is not working. I'm using version 1.7.3 of both socket.io and socket.io-client.

ibrahimduran commented Apr 16, 2017

I'm currently having the same issue with my node application too. I use built-in server of socket.io and it seems to work fine in browser but in node app socket.io-client throws the same 503 xhr poll error above. I'm running server and client on the same machine but connecting to loopback or local network address from client application is not working. I'm using version 1.7.3 of both socket.io and socket.io-client.

@SupremeTechnopriest

This comment has been minimized.

Show comment
Hide comment
@SupremeTechnopriest

SupremeTechnopriest May 14, 2017

l also see this error in version 2.0.1 (client and server) when running from node.

SupremeTechnopriest commented May 14, 2017

l also see this error in version 2.0.1 (client and server) when running from node.

@ibrahimduran

This comment has been minimized.

Show comment
Hide comment
@ibrahimduran

ibrahimduran May 14, 2017

I've solved my problem a week ago by refactoring but I don't remember what exactly solved the problem. Sorry for that, I had a busy week and that was the initial commit.

You can try setting opts.transports to only websocket in client:

io.connect(url, { transports: ['websocket'] }); // default is ['polling', 'websocket']

I had too complicated socket and action binding logic before I refactored the code. So make sure your socket logic in your code is working and make it simpler to debug.

ibrahimduran commented May 14, 2017

I've solved my problem a week ago by refactoring but I don't remember what exactly solved the problem. Sorry for that, I had a busy week and that was the initial commit.

You can try setting opts.transports to only websocket in client:

io.connect(url, { transports: ['websocket'] }); // default is ['polling', 'websocket']

I had too complicated socket and action binding logic before I refactored the code. So make sure your socket logic in your code is working and make it simpler to debug.

@SupremeTechnopriest

This comment has been minimized.

Show comment
Hide comment
@SupremeTechnopriest

SupremeTechnopriest May 14, 2017

I'll try the we transport. For now I've downgraded to 1.7.4 and all seems to work.

SupremeTechnopriest commented May 14, 2017

I'll try the we transport. For now I've downgraded to 1.7.4 and all seems to work.

@wangzhiqiang1975

This comment has been minimized.

Show comment
Hide comment
@wangzhiqiang1975

wangzhiqiang1975 May 16, 2017

Yes I down version from 2.0.1 to 1.7.4 fix this

wangzhiqiang1975 commented May 16, 2017

Yes I down version from 2.0.1 to 1.7.4 fix this

@SupremeTechnopriest

This comment has been minimized.

Show comment
Hide comment
@SupremeTechnopriest

SupremeTechnopriest May 17, 2017

So I've upgraded back to 2.0.1 and I set up my client to use the websocket transport only. It (almost) works:

io(`localhost:8080`, {
	transports: [ 'websocket' ],
	upgrade: false
});

Now my only problem is that the websocket transport is broken for web worker scopes. I think I've identified the problem and opened a corresponding issue on engine.io-client here socketio/engine.io-client#569

SupremeTechnopriest commented May 17, 2017

So I've upgraded back to 2.0.1 and I set up my client to use the websocket transport only. It (almost) works:

io(`localhost:8080`, {
	transports: [ 'websocket' ],
	upgrade: false
});

Now my only problem is that the websocket transport is broken for web worker scopes. I think I've identified the problem and opened a corresponding issue on engine.io-client here socketio/engine.io-client#569

@darrachequesne

This comment has been minimized.

Show comment
Hide comment
@darrachequesne

darrachequesne May 19, 2017

Contributor

Is there any error thrown? Could you run with debug enabled? (DEBUG=engine*)

Regarding react-native, that may be linked to #1114.

Contributor

darrachequesne commented May 19, 2017

Is there any error thrown? Could you run with debug enabled? (DEBUG=engine*)

Regarding react-native, that may be linked to #1114.

@SupremeTechnopriest

This comment has been minimized.

Show comment
Hide comment
@SupremeTechnopriest

SupremeTechnopriest May 19, 2017

@darrachequesne So I just reverted to default transports on my server side client component. My mistake there... The polling transport is definitely working for me. The issue I had was with my self signed ssl certs for local dev. I had to add rejectUnauthorized: process.env.NODE_ENV === 'production' to the socket.io config to make it work. I was getting a 503 otherwise.

The web worker issue is real though. socketio/engine.io-client#569

SupremeTechnopriest commented May 19, 2017

@darrachequesne So I just reverted to default transports on my server side client component. My mistake there... The polling transport is definitely working for me. The issue I had was with my self signed ssl certs for local dev. I had to add rejectUnauthorized: process.env.NODE_ENV === 'production' to the socket.io config to make it work. I was getting a 503 otherwise.

The web worker issue is real though. socketio/engine.io-client#569

@zetsin

This comment has been minimized.

Show comment
Hide comment
@zetsin

zetsin May 20, 2017

try this

require('socket.io-client')('http://localhost', {
  rejectUnauthorized:   false,
})

zetsin commented May 20, 2017

try this

require('socket.io-client')('http://localhost', {
  rejectUnauthorized:   false,
})
@wangzhiqiang1975

This comment has been minimized.

Show comment
Hide comment
@wangzhiqiang1975

wangzhiqiang1975 May 23, 2017

wangzhiqiang1975 commented May 23, 2017

@CrisLi

This comment has been minimized.

Show comment
Hide comment
@CrisLi

CrisLi Jul 18, 2017

I have the same issue. Then I change my client code to

require('socket.io-client')('wss://myserver:port/ws', { transports: ['websocket'], rejectUnauthorized: false });

But now, I get a 404 error response. The log show that there is an additional /socket.io/ added to my request url.

 { Error: unexpected server response (404)
       at ClientRequest._req.on (/Users/chris/Projects/nationsky/mgmt-server/backend/node_modules/ws/lib/WebSocket.js:650:26)
       at emitOne (events.js:115:13)
       at ClientRequest.emit (events.js:210:7)
       at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:564:21)
       at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
       at TLSSocket.socketOnData (_http_client.js:453:20)
       at emitOne (events.js:115:13)
       at TLSSocket.emit (events.js:210:7)
       at addChunk (_stream_readable.js:252:12)
       at readableAddChunk (_stream_readable.js:239:11)
     type: 'error',
     target:
      WebSocket {
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        readyState: 0,
        bytesReceived: 0,
        extensions: {},
        protocol: '',
        _binaryType: 'arraybuffer',
        _finalize: [Function: bound finalize],
        _finalizeCalled: false,
        _closeMessage: null,
        _closeTimer: null,
        _closeCode: null,
        _receiver: null,
        _sender: null,
        _socket: null,
        _ultron: null,
        protocolVersion: 13,
        _isServer: false,
        url: 'wss://myserver:port/socket.io/?access_token=HnbtDUEWu1i1HVrAn-DO9YOL6GZxoittWAf8Sz_RR0s&EIO=3&transport=websocket',
        _req: [Object] } } }

CrisLi commented Jul 18, 2017

I have the same issue. Then I change my client code to

require('socket.io-client')('wss://myserver:port/ws', { transports: ['websocket'], rejectUnauthorized: false });

But now, I get a 404 error response. The log show that there is an additional /socket.io/ added to my request url.

 { Error: unexpected server response (404)
       at ClientRequest._req.on (/Users/chris/Projects/nationsky/mgmt-server/backend/node_modules/ws/lib/WebSocket.js:650:26)
       at emitOne (events.js:115:13)
       at ClientRequest.emit (events.js:210:7)
       at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:564:21)
       at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
       at TLSSocket.socketOnData (_http_client.js:453:20)
       at emitOne (events.js:115:13)
       at TLSSocket.emit (events.js:210:7)
       at addChunk (_stream_readable.js:252:12)
       at readableAddChunk (_stream_readable.js:239:11)
     type: 'error',
     target:
      WebSocket {
        domain: null,
        _events: [Object],
        _eventsCount: 4,
        _maxListeners: undefined,
        readyState: 0,
        bytesReceived: 0,
        extensions: {},
        protocol: '',
        _binaryType: 'arraybuffer',
        _finalize: [Function: bound finalize],
        _finalizeCalled: false,
        _closeMessage: null,
        _closeTimer: null,
        _closeCode: null,
        _receiver: null,
        _sender: null,
        _socket: null,
        _ultron: null,
        protocolVersion: 13,
        _isServer: false,
        url: 'wss://myserver:port/socket.io/?access_token=HnbtDUEWu1i1HVrAn-DO9YOL6GZxoittWAf8Sz_RR0s&EIO=3&transport=websocket',
        _req: [Object] } } }
@cosminn777

This comment has been minimized.

Show comment
Hide comment
@cosminn777

cosminn777 Sep 21, 2017

For me it appears even if debugger in not present

cosminn777 commented Sep 21, 2017

For me it appears even if debugger in not present

@ntrrg

This comment has been minimized.

Show comment
Hide comment
@ntrrg

ntrrg Oct 23, 2017

Hi! i had the same problem while i was trying to connect from a Node.js app to the server, here's my code:

const socket = require('socket.io-client')('https://domain.com')

The log that i got running DEBUG=engine* npm run dev was some like this:

asciicast

engine.io-client:socket creating transport "polling" +2s
engine.io-client:polling polling +1ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://domain.com/socket.io/?EIO=3&transport=polling&t=LzBM71r&b64=1 +0ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +1ms
engine.io-client:socket socket error {"type":"TransportError","description":503} +7ms
engine.io-client:socket socket close with reason: "transport error" +0ms
engine.io-client:polling transport not open - deferring close +0ms

When i tried connect from a browser to https://domain.com/socket.io/socket.io.js it worked and using io('https://domain.com') too, so i followed the instructions in the comments and i changed my code to:

const socket = require('socket.io-client')('https://domain.com', { rejectUnauthorized: false })

And it worked, but it's weird, i have valid certs from Let's Encrypt, or it's normal? anyway.. i added the transports option (from the comments too) for avoiding other connections than websocket and at the end the code was:

const socket = require('socket.io-client')('https://domain.com', {
  transports: ['websocket'],
  rejectUnauthorized: false
})

Thanks @ibrahimduran and @SupremeTechnopriest 😄

My apologies if i wrote like Tarzan 😅 i working on my english haha

ntrrg commented Oct 23, 2017

Hi! i had the same problem while i was trying to connect from a Node.js app to the server, here's my code:

const socket = require('socket.io-client')('https://domain.com')

The log that i got running DEBUG=engine* npm run dev was some like this:

asciicast

engine.io-client:socket creating transport "polling" +2s
engine.io-client:polling polling +1ms
engine.io-client:polling-xhr xhr poll +0ms
engine.io-client:polling-xhr xhr open GET: https://domain.com/socket.io/?EIO=3&transport=polling&t=LzBM71r&b64=1 +0ms
engine.io-client:polling-xhr xhr data null +0ms
engine.io-client:socket setting transport polling +1ms
engine.io-client:socket socket error {"type":"TransportError","description":503} +7ms
engine.io-client:socket socket close with reason: "transport error" +0ms
engine.io-client:polling transport not open - deferring close +0ms

When i tried connect from a browser to https://domain.com/socket.io/socket.io.js it worked and using io('https://domain.com') too, so i followed the instructions in the comments and i changed my code to:

const socket = require('socket.io-client')('https://domain.com', { rejectUnauthorized: false })

And it worked, but it's weird, i have valid certs from Let's Encrypt, or it's normal? anyway.. i added the transports option (from the comments too) for avoiding other connections than websocket and at the end the code was:

const socket = require('socket.io-client')('https://domain.com', {
  transports: ['websocket'],
  rejectUnauthorized: false
})

Thanks @ibrahimduran and @SupremeTechnopriest 😄

My apologies if i wrote like Tarzan 😅 i working on my english haha

@teja42

This comment has been minimized.

Show comment
Hide comment
@teja42

teja42 Jan 13, 2018

I've had this issue and I spent 2 hours on this, turns out it's just specifying your options correctly. Here is how you should specify them :

   let socketClient = socketIOClient.connect(`http://localhost:3001`,{ // [1] Important as fuck 
      reconnectionDelay: 1000,
      reconnection:true,
      reconnectionAttempts: 10,
      transports: ['websocket'],
      agent: false, // [2] Please don't set this to true
      upgrade: false,
      rejectUnauthorized: false
   });

[1] If you don't specify whether to use http or https , it defaults to https . Or if you dont specify the port right after the url as "url.com:port" it defaults to 80 or 443 depending on the protocol. I've seen some stackoverflow answers that have port specified as an option in the options object. I've looked into the code and there is no such option. You need to include the port in the url.

[2] And also, I set the agent to true and it failed to establish a ws connection and there was no error, it simply timed out. Just don't specify the agent at all. It defaults to false.

teja42 commented Jan 13, 2018

I've had this issue and I spent 2 hours on this, turns out it's just specifying your options correctly. Here is how you should specify them :

   let socketClient = socketIOClient.connect(`http://localhost:3001`,{ // [1] Important as fuck 
      reconnectionDelay: 1000,
      reconnection:true,
      reconnectionAttempts: 10,
      transports: ['websocket'],
      agent: false, // [2] Please don't set this to true
      upgrade: false,
      rejectUnauthorized: false
   });

[1] If you don't specify whether to use http or https , it defaults to https . Or if you dont specify the port right after the url as "url.com:port" it defaults to 80 or 443 depending on the protocol. I've seen some stackoverflow answers that have port specified as an option in the options object. I've looked into the code and there is no such option. You need to include the port in the url.

[2] And also, I set the agent to true and it failed to establish a ws connection and there was no error, it simply timed out. Just don't specify the agent at all. It defaults to false.

@iamjosan

This comment has been minimized.

Show comment
Hide comment
@iamjosan

iamjosan Feb 3, 2018

@teja42 I tried fixing this problem for 2 days. I thought it was a bundling issue with webpack but your solution worked wonderfully. Thanks!

iamjosan commented Feb 3, 2018

@teja42 I tried fixing this problem for 2 days. I thought it was a bundling issue with webpack but your solution worked wonderfully. Thanks!

@josepichu

This comment has been minimized.

Show comment
Hide comment
@josepichu

josepichu Apr 10, 2018

Hi !

I've had the same problem and fixed downgrading from 2.0.1 to 1.7.4 as @SupremeTechnopriest told and all works fine !

thanks !

josepichu commented Apr 10, 2018

Hi !

I've had the same problem and fixed downgrading from 2.0.1 to 1.7.4 as @SupremeTechnopriest told and all works fine !

thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment