socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

On aws server side getting 404 not found error

Open sampete1997 opened this issue 2 years ago • 1 comments

Hi I have node js server with express and react js client in local. in local socket io is working fine but when i deployed to aws ec2 instance I am getting 404 status error

server

import express, { Express, Response, Request, NextFunction } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import ErrorHandler from "./utils/errorHandler";
import MongoService, { Collections } from "./utils/mongo";
import http from 'http'
import socketIO from "socket.io";

const app: Express = express();
const PORT = process.env.PORT;

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: "1mb" }));
app.use(cors());
const server = http.createServer(app);

export const io = new socketIO.Server(server, {
  cors: {
    origin: "*",  
    methods: ["GET", "POST"],
  },
});

io.on("connection", (socket) => {
    console.log(
      "connection successful! \n user connected ID: ",
      socket.id,
      "\n"
    );
    
    socket.on("disconnect", () => {
      console.log("Disconnected", "\n user ID: ", socket.id, "\n");
    });
  });
export const IO = io;

MongoService.init().then(() => {
  server.listen(PORT, async () => {
    await seed();
    console.log(`Server running at ${PORT}`);
  });
});

client

import io from 'socket.io-client';
// export const BACKEND_URL = 'http://localhost:3001';
export const BACKEND_URL = 'https://mywsbackend.tech';
export const socket = io(BACKEND_URL,{withCredentials: false, rejectUnauthorized: false}); //connecting to socket server
socket.on("connect_error", (err) => {
  console.log(`connect_error due to ${err.message}`);
});

aws ec2 logs

0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8PKY 404 149 - 0.493 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8PL1 404 149 - 0.417 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8Pjr 404 149 - 0.447 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8Qme 404 149 - 0.459 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8RBb 404 149 - 0.451 ms
0|index  | GET / 200 24 - 0.487 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8SEW 404 149 - 0.438 ms
0|index  | GET /socket.io/?EIO=4&transport=polling&t=OeD8SF0 404 149 - 0.417 ms

REQUEST HEADER

Request URL:
https://mywsbackend.tech/socket.io/?EIO=4&transport=polling&t=OeD7QRi
Request Method:
GET
Status Code:
404
Remote Address:
#########
Referrer Policy:
strict-origin-when-cross-origin

:Authority:
mywsbackend.tech
:Method:
GET
:Path:
/socket.io/?EIO=4&transport=polling&t=OeD7QRi
:Scheme:
https
Accept:
*/*
Accept-Encoding:
gzip, deflate, br
Accept-Language:
en-US,en;q=0.9
Origin:
http://localhost:3000
Referer:
http://localhost:3000/
Sec-Fetch-Dest:
empty
Sec-Fetch-Mode:
cors
Sec-Fetch-Site:
cross-site

everything is working fine in local getting issue in aws server only please help to fix this!

Originally posted by @sampete1997 in https://github.com/socketio/socket.io/issues/3615#issuecomment-1684912436

sampete1997 avatar Aug 19 '23 10:08 sampete1997

@sampete1997 You're not telling us how you're serving the socket.io. If your express server / socket.io server running on port 443? Or is it 3000?

Normally you need a reverse proxy and also need to proxy the websockets like this: https://socket.io/docs/v4/reverse-proxy/

If you can't access the following URL: https://mywsbackend.tech/socket.io/socket.io.js , then it means you're not properly serving socket.io to the publicly accessible port.

Try maybe https://mywsbackend.tech:3000/socket.io/socket.io.js as a test

I also recommend adding the origin names of what will access your server instead of origin: "*", despite setting the credentials required to false, i'm not sure you can still do a origin wildcard in socket v4.

y4my4my4m avatar Aug 20 '23 06:08 y4my4my4m