Skip to content

Commit 2071a66

Browse files
docs: simplify nginx cluster example
- remove useless Dockerfile - clean format - migrate to @socket.io/redis-adapter
1 parent 0f11c47 commit 2071a66

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

examples/cluster-nginx/docker-compose.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
nginx:
3-
build: ./nginx
3+
image: nginx:alpine
4+
volumes:
5+
- ./nginx.conf:/etc/nginx/nginx.conf:ro
46
links:
57
- server-john
68
- server-paul

examples/cluster-nginx/nginx/Dockerfile

Lines changed: 0 additions & 3 deletions
This file was deleted.

examples/cluster-nginx/server/index.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
// Setup basic express server
2-
var express = require('express');
3-
var app = express();
4-
var server = require('http').createServer(app);
5-
var io = require('socket.io')(server);
6-
var redis = require('socket.io-redis');
7-
var port = process.env.PORT || 3000;
8-
var serverName = process.env.NAME || 'Unknown';
1+
const express = require('express');
2+
const app = express();
3+
const server = require('http').createServer(app);
4+
const io = require('socket.io')(server);
5+
const { createAdapter } = require('@socket.io/redis-adapter');
6+
const { createClient } = require('redis');
7+
const port = process.env.PORT || 3000;
8+
const serverName = process.env.NAME || 'Unknown';
99

10-
io.adapter(redis({ host: 'redis', port: 6379 }));
10+
const pubClient = createClient({ host: 'redis', port: 6379 });
11+
const subClient = pubClient.duplicate();
1112

12-
server.listen(port, function () {
13+
io.adapter(createAdapter(pubClient, subClient));
14+
15+
server.listen(port, () => {
1316
console.log('Server listening at port %d', port);
1417
console.log('Hello, I\'m %s, how can I help?', serverName);
1518
});
@@ -19,15 +22,15 @@ app.use(express.static(__dirname + '/public'));
1922

2023
// Chatroom
2124

22-
var numUsers = 0;
25+
let numUsers = 0;
2326

24-
io.on('connection', function (socket) {
27+
io.on('connection', socket => {
2528
socket.emit('my-name-is', serverName);
2629

27-
var addedUser = false;
30+
let addedUser = false;
2831

2932
// when the client emits 'new message', this listens and executes
30-
socket.on('new message', function (data) {
33+
socket.on('new message', data => {
3134
// we tell the client to execute 'new message'
3235
socket.broadcast.emit('new message', {
3336
username: socket.username,
@@ -36,7 +39,7 @@ io.on('connection', function (socket) {
3639
});
3740

3841
// when the client emits 'add user', this listens and executes
39-
socket.on('add user', function (username) {
42+
socket.on('add user', username => {
4043
if (addedUser) return;
4144

4245
// we store the username in the socket session for this client
@@ -54,21 +57,21 @@ io.on('connection', function (socket) {
5457
});
5558

5659
// when the client emits 'typing', we broadcast it to others
57-
socket.on('typing', function () {
60+
socket.on('typing', () => {
5861
socket.broadcast.emit('typing', {
5962
username: socket.username
6063
});
6164
});
6265

6366
// when the client emits 'stop typing', we broadcast it to others
64-
socket.on('stop typing', function () {
67+
socket.on('stop typing', () => {
6568
socket.broadcast.emit('stop typing', {
6669
username: socket.username
6770
});
6871
});
6972

7073
// when the user disconnects.. perform this
71-
socket.on('disconnect', function () {
74+
socket.on('disconnect', () => {
7275
if (addedUser) {
7376
--numUsers;
7477

examples/cluster-nginx/server/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"private": true,
88
"license": "MIT",
99
"dependencies": {
10+
"@socket.io/redis-adapter": "^7.0.1",
1011
"express": "4.13.4",
11-
"socket.io": "^4.0.0",
12-
"socket.io-redis": "^6.0.1"
12+
"redis": "^3.1.2",
13+
"socket.io": "^4.0.0"
1314
},
1415
"scripts": {
1516
"start": "node index.js"

0 commit comments

Comments
 (0)