6

Background

I have a Node.js server using socket.io that accepts connections from clients via HTTPS.

I know this server works as I am able to connect to it via browser.

Problem

The problem is that I can't create a node app to connect to this server as a client.

I am using the following code:

const io = require("socket.io-client");

const socket = io.connect("https://my.website.com:3002", { secure: true, reconnect: true });

socket.on("connect", function(){
    console.log("connected");
});

socket.on("disconnect", function(){
    console.log("disconnected");
});

socket.on("error", console.error);

The server registers no connections, and this app logs no errors. It would seem that I am connecting to the wrong server, but this same URL works just fine when I use a browser.

Research

I have searched github and the official docs for an answer. Even similar questions from stackoverflow seem to not work:

Question

What am I doing wrong ?

5
  • I have the exact same code on browser and it works. This is what I find hard to understand. Commented Dec 7, 2017 at 13:40
  • btw running your code results in no output for me too :D Commented Dec 7, 2017 at 13:50
  • 1
    I got an output adding: socket.on("connect_error", function(e){ console.log("connect_error", e); }); and waiting 20 sec. Looks like the error event is not emited for all types of errors -.- Commented Dec 7, 2017 at 13:56
  • 1
    @RolandStarke That information is quite precious. Thanks ! Commented Dec 7, 2017 at 14:14
  • I think it might be important to mention specifically the use of https in the title, as I spent 2 days hunting for this solution Commented Apr 8, 2018 at 4:52

2 Answers 2

20

Answer

After realising, that not all errors feed into the "error" event ( special thanks to @RolandStarke ) I found that I was having a consistent XHR pool request:

{ Error: xhr poll error
    at XHR.Transport.onError (/Users/pedro/Workspace/backend-stresser/node_modules/engine.io-client/lib/transport.js:64:13)
    at Request.<anonymous> (/Users/pedro/Workspace/backend-stresser/node_modules/engine.io-client/lib/transports/polling-xhr.js:128:10)
    at Request.Emitter.emit (/Users/pedro/Workspace/backend-stresser/node_modules/component-emitter/index.js:133:20)
    at Request.onError (/Users/pedro/Workspace/backend-stresser/node_modules/engine.io-client/lib/transports/polling-xhr.js:310:8)
    at Timeout._onTimeout (/Users/pedro/Workspace/backend-stresser/node_modules/engine.io-client/lib/transports/polling-xhr.js:257:18)
    at ontimeout (timers.js:469:11)
    at tryOnTimeout (timers.js:304:5)
    at Timer.listOnTimeout (timers.js:264:5) type: 'TransportError', description: 503 }

Once I had this information, I made a quick search and found a solution to this issue, which seems to be a bug:

The code I am now using is:

const socket = io.connect("https://my.website.com:3002", { secure: true, reconnection: true, rejectUnauthorized: false });

And it works as expected.

Sign up to request clarification or add additional context in comments.

4 Comments

After 2 days of bashing my head against a wall, this answer saved my arse. NONE of the other examples/answers that I found show how to do it with https
Good to know I helped :P
@Flame_Phoenix I am getting Parse Error HPE_INVALID_CONSTANT any clue?
For me the key is rejectUnauthorized: false because I'm using a self signed certificate.
3

I have used this code in my client side and it worked:

import io from "socket.io-client"
const SERVER = "http://localhost:5000"
const socket = io(SERVER, { transports: ["websocket"] })

1 Comment

Adding transports parameters in socket constructor helped me a lot! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.