everyone!
I am trying to deploy my application, which uses React and NodeJs. The thing is, I'm using Socket.io for some data handling, and it works fine, but I don't understand how to make it work with Nginx.
I'm editing the Nhinx file with:
sudo nano /etc/nginx/sites-available/react
And this is the code:
server {
server_name {my ip};
root /root/my-app/build;
index index.html index.htm;
location / {
try_files $uri /index.html =404;
}
location /socketio {
proxy_pass http://localhost:9000;
include proxy_params;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
}
Socked code in client:
import { io } from "socket.io-client";
let socket = io('http://159.223.113.224',{
path: '/socketio',
});
export default socket;
Socked code in server:
"use strict";
exports.__esModule = true;
var express = require("express");
var http = require('http');
var appSocket = express();
var server = http.createServer(appSocket);
var io = require("socket.io")(server, {
path: "/socketio",
cors: {
origin: "*",
methods: ["GET", "POST"],
allowedHeaders: ["my-custom-header"],
credentials: true
}
});
var rooms = require("./game/socketLogic/rooms")(io);
var gameLogic = require("./game/socketLogic/gameLogic")(io);
var teamChat = require("./game/socketLogic/teamChat")(io);
var timer = require("./game/socketLogic/timer")(io);
var lobbyChat = require("./game/socketLogic/lobbyChat")(io);
require("reflect-metadata");
var cors = require("cors");
const { type } = require("os");
appSocket.use(express.json());
appSocket.use(express.urlencoded({ extended: false }));
appSocket.use(cors());
server.listen(9000, function () {
console.log('Server listening at port 9000.');
});
I don't know if I'm forgetting something. I also have the previous file running with pm2. When I enter to the page, the next error appears:
GET http://localhost:9000/socket.io/?EIO=4&transport=polling&t=NozbkRz net::ERR_CONNECTION_REFUSED
I solved the problem, I didn't know that I should run "npm run build" again. I leave my answer here in case it helps someone else. Greetings!