Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Socket.io] How to using socket.io with pm2 cluster mode #2438

Open
sonpham-bmd opened this issue Sep 20, 2023 · 6 comments
Open

[Socket.io] How to using socket.io with pm2 cluster mode #2438

sonpham-bmd opened this issue Sep 20, 2023 · 6 comments
Assignees

Comments

@sonpham-bmd
Copy link

sonpham-bmd commented Sep 20, 2023

Is your feature request related to a problem? Please describe.

I'm using socket.io with pm2 cluster mode (4 instances), when the socket init on multiple nodes, it's can not merge all socket in one. I read docs https://socket.io/docs/v4/pm2/, but it's use http module. Please help me check this case. Thank you so much! 🙏

Describe the solution you'd like

No response

Describe alternatives you've considered

No response

Additional context

No response

Acceptance criteria

No response

@Romakita
Copy link
Collaborator

Romakita commented Sep 20, 2023

Hello @sonpham-bmd

You can try this solution:

import {Server as IOServer, ServerOptions} from "socket.io";
import { createAdapter } from "@socket.io/cluster-adapter";
import { setupWorker } from "@socket.io/sticky";

@Configuration()
class Server {
  @IO()
  private io: IOServer;
  
  $afterSocketListen() {
     this.io.adapter(createAdapter());

     setupWorker(this.io);
  } 
}

See you

@Romakita
Copy link
Collaborator

Note: Github discussion is more appropriate for this topic

@sonpham-bmd
Copy link
Author

Hello @sonpham-bmd

You can try this solution:

import {Server as IOServer, ServerOptions} from "socket.io";
import { createAdapter } from "@socket.io/cluster-adapter";
import { setupWorker } from "@socket.io/sticky";

@Configuration()
class Server {
  @IO()
  private io: IOServer;
  
  $afterSocketListen() {
     this.io.adapter(createAdapter());

     setupWorker(this.io);
  } 
}

See you

Screenshot 2023-09-20 at 14 37 27

after add, server can not start.

@Romakita
Copy link
Collaborator

ok I don't know why. did you start the process with pm2 ?

@sonpham-bmd
Copy link
Author

sonpham-bmd commented Sep 20, 2023

ok I don't know why. did you start the process with pm2 ?

I run pm2 with ecosystem file js
module.exports = { apps: [ { name: '248-backend', exec_mode: 'cluster', instances: 2, script: './dist/src/index.js', args: 'start', watch: true } ] }

@sonpham-bmd sonpham-bmd reopened this Sep 20, 2023
@tsedio tsedio deleted a comment from github-actions bot Sep 20, 2023
@Romakita
Copy link
Collaborator

Romakita commented Sep 20, 2023

@sonpham-bmd Ok can you create small repo example please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants