user10654908
user10654908

Reputation: 85

socket.io: 404 errors in javascript console

In google chrome, example.com/socket.io gives 404 errors. So it seems like my socket.io configuration has errors. I have no idea what they are. Here are the most important bits of my node.js server:

Server.js

"use strict";
/** Imported modules */
const express = require("express");
const server = express();
var app = require("http").createServer(server);

const io = require("socket.io")(app);

const request = require("./modules/request.js");
const db = require("./modules/database.js")(bcrypt, sequelize);
const io_control = require("./modules/io_control.js")(io, db, sequelize);

/** Page Control Modules */
const PageConfig = require("./modules/page/PageConfig.js");
const CommonItems = require("./modules/page/CommonItems.js");

server.use(express.urlencoded({ extended: true }));
io.origins("*:*");
const Port = process.env.PORT || 3000;
app.listen(Port);

io.on("connection", function(socket) {
  console.log(true);
});

io_control.js

module.exports = (io, db, sequelize) => {
  var controller = new IOControl(io, db, sequelize);

  io.on("connection", function(socket) {
    console.log(true);
    //and a lot of other code inside here.
  });
};

On client side: it goes like this:

First script

<script src="/socket.io" type="application/javascript"></script>

Second script

const socket = io('//web.namei.nl',{path: '/socket.io'},{transports: ['websocket'], upgrade: false}).connect('', {query: `uuid=${getCookie('uuid')}&type=${getCookie('type')}` });

I tried altering both scripts, no result.

Upvotes: 0

Views: 341

Answers (1)

Samuel Goldenbaum
Samuel Goldenbaum

Reputation: 18909

You are getting a 404 due to the socket.io client script source path: src="/socket.io" which is probably not resolving as the path does not seem valid.

Either use a CDN, point to the location at your IO server like: //web.namei.nl/socket.io/socket.io.js or download and include the file in your client directly or using a bundler.

UPDATE after additional comments from OP

If you are unable to connect to the socket server and are sure the URL is correct, then:

  1. As you are using express to host your IO, you should enable CORS
  2. Try connect without using websockets as the transport as there may be a limitation at your hosting service.
  3. Ensure you connect options are configured correctly - they don't appear to be correct.
  4. Only use io.origins if you want to limit which domains can access the server.

At server:

const cors = require('cors');
...
const app = express();
app.use(cors());

const http = require('http').createServer(app);
const io = require('socket.io')(http);

io.on('connection', (socket) => {
   ...
});

const port = process.env.PORT || 3000;
http.listen(port, () => {
    console.log(`listening on ${port}`);
});

Client:

// ensure the URL including port are correct below, also first connect without the additional query option
const socket = io('https://web.namei.nl', {
   query: `uuid=${getCookie('uuid')}&type=${getCookie('type')}`
});

socket.on('connection', () => {
   console.info('connected');
});

Upvotes: 1

Related Questions