-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): 4th and final part of the "private messaging" example
- Loading branch information
1 parent
7247b40
commit 7467216
Showing
6 changed files
with
156 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const cluster = require("cluster"); | ||
const http = require("http"); | ||
const { setupMaster } = require("@socket.io/sticky"); | ||
|
||
const WORKERS_COUNT = 4; | ||
|
||
if (cluster.isMaster) { | ||
console.log(`Master ${process.pid} is running`); | ||
|
||
for (let i = 0; i < WORKERS_COUNT; i++) { | ||
cluster.fork(); | ||
} | ||
|
||
cluster.on("exit", (worker) => { | ||
console.log(`Worker ${worker.process.pid} died`); | ||
cluster.fork(); | ||
}); | ||
|
||
const httpServer = http.createServer(); | ||
setupMaster(httpServer, { | ||
loadBalancingMethod: "least-connection", // either "random", "round-robin" or "least-connection" | ||
}); | ||
const PORT = process.env.PORT || 3000; | ||
|
||
httpServer.listen(PORT, () => | ||
console.log(`server listening at http://localhost:${PORT}`) | ||
); | ||
} else { | ||
console.log(`Worker ${process.pid} started`); | ||
require("./index"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: "3" | ||
|
||
services: | ||
redis: | ||
image: redis:5 | ||
ports: | ||
- "6379:6379" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,14 @@ | |
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "node index.js" | ||
"start": "node cluster.js" | ||
}, | ||
"author": "Damien Arrachequesne <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"socket.io": "^3.1.1" | ||
"@socket.io/sticky": "^1.0.0", | ||
"ioredis": "^4.22.0", | ||
"socket.io": "^3.1.1", | ||
"socket.io-redis": "^6.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters