Adam Baranyai
Adam Baranyai

Reputation: 3867

Can't setup socket.io connection with the browser using nodejs

I am getting a strange error when trying to set up a nodejs - browser socket.io connection.

I've migrated out the socket.io related code to a file, here it is:

import socketIO from 'socket.io';
import https from 'https';
import express from 'express';
import fs from 'fs';

var listener,
    io;

listener =  https.createServer({
    key: fs.readFileSync('/path/to/key'),
    cert: fs.readFileSync('/path/to/crt')
}, express());


listener.listen(3200, function() {
    console.log("Listening on 3200"); 
    io = socketIO(listener, {
      rejectUnauthorized: false,
      wsEngine: "ws"
    });

    io.on('connection', function(socket){
        console.log('a user connected');
      });
});

If I run this file with node main.js, the console outputs the following message:

(node:615) ExperimentalWarning: The ESM module loader is experimental.
Listening on 3200

Now if I try to navigate with a browser to https://my.domain.name:3200, the browser outputs the following message:

Cannot GET /

And no output is received from the node server.

What could be causing this issue?

Upvotes: 0

Views: 93

Answers (1)

herohamp
herohamp

Reputation: 644

You have to create a web socket connection to https://my.domain.name:3200, not just go to the page. Try following https://socket.io/get-started/chat/, it will show how to serve a web page to make that connection from.

Upvotes: 1

Related Questions