0

This is my first experience with socket. My project stack: NextJS(v10), express, socket.io, socket.io-client, freeice

Everything works fine on localhost. The problem arose when I decided to test on a test server(started project with PM2).

server.ts

const nextApp = next({ dev: config.dev });
const app = express();
const server = require('http').Server(app);
const io = require('socket.io')(server);
io.on('connection', socket => console.log('connected')); // did not write the rest of the code here, because I don't even get the console.log

on client side

import { io } from 'socket.io-client';
const options = {
   'force_new_connection': true,
   reconnectionAttempts: 'Infinity',
   timeout: 10000,
   transports: ['websocket']
};
const socket = io('http s://mysite.com:3000', options);

what i get in a short time:

WebSocket connection to '' failed: WebSocket is closed before the connection is established.

The first and probably the most banal question due to my inexperience is whether I should configure anything on the Amazon server itself (probably said nonsense) and whether it can somehow have an impact PM2?

Second, here const server = require('http').Server(app);. It is problem?

I checked, I don't call close() anywhere, it happens here: enter image description here

Thanks for any help

I found another small difference between a local test and a site test. I am getting two different objects. What is lt?

const socket = io(connectionUrl, options);
console.log('socket: ', socket);

localhost: enter image description here

on mysite.com: enter image description here

Roman Nozhenko
  • 698
  • 8
  • 21
  • For your second question check https://stackoverflow.com/questions/26921117/http-createserverapp-v-http-serverapp and for your first question which socket.io-client version you are using ? – Qiulang Jun 30 '22 at 10:19
  • "socket.io": "^4.5.1", "socket.io-client": "^4.5.1", – Roman Nozhenko Jul 01 '22 at 04:32
  • You probably need to past the full code to let other further investigate (or better create a github project to reproduce your problem). But if you just experience it you better not use https (as in your code pasted here) – Qiulang Jul 01 '22 at 07:13
  • I won't be able to put it on github in its entirety, it's an existing big project that I decided to add a video chat to. The strange thing is that it works locally (yarn dev - OK, build + PM2 start - OK too) Which suggests that it is here that some kind of nuance is hidden, I’ll check with the system administrator, maybe there are some nuances on the server itself, I’m not good at this ( – Roman Nozhenko Jul 01 '22 at 07:29
  • But if it helps, I can post almost the complete server.ts code, and client page, that used socket – Roman Nozhenko Jul 01 '22 at 07:35

0 Answers0