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' ;
9
9
10
- io . adapter ( redis ( { host : 'redis' , port : 6379 } ) ) ;
10
+ const pubClient = createClient ( { host : 'redis' , port : 6379 } ) ;
11
+ const subClient = pubClient . duplicate ( ) ;
11
12
12
- server . listen ( port , function ( ) {
13
+ io . adapter ( createAdapter ( pubClient , subClient ) ) ;
14
+
15
+ server . listen ( port , ( ) => {
13
16
console . log ( 'Server listening at port %d' , port ) ;
14
17
console . log ( 'Hello, I\'m %s, how can I help?' , serverName ) ;
15
18
} ) ;
@@ -19,15 +22,15 @@ app.use(express.static(__dirname + '/public'));
19
22
20
23
// Chatroom
21
24
22
- var numUsers = 0 ;
25
+ let numUsers = 0 ;
23
26
24
- io . on ( 'connection' , function ( socket ) {
27
+ io . on ( 'connection' , socket => {
25
28
socket . emit ( 'my-name-is' , serverName ) ;
26
29
27
- var addedUser = false ;
30
+ let addedUser = false ;
28
31
29
32
// when the client emits 'new message', this listens and executes
30
- socket . on ( 'new message' , function ( data ) {
33
+ socket . on ( 'new message' , data => {
31
34
// we tell the client to execute 'new message'
32
35
socket . broadcast . emit ( 'new message' , {
33
36
username : socket . username ,
@@ -36,7 +39,7 @@ io.on('connection', function (socket) {
36
39
} ) ;
37
40
38
41
// when the client emits 'add user', this listens and executes
39
- socket . on ( 'add user' , function ( username ) {
42
+ socket . on ( 'add user' , username => {
40
43
if ( addedUser ) return ;
41
44
42
45
// we store the username in the socket session for this client
@@ -54,21 +57,21 @@ io.on('connection', function (socket) {
54
57
} ) ;
55
58
56
59
// when the client emits 'typing', we broadcast it to others
57
- socket . on ( 'typing' , function ( ) {
60
+ socket . on ( 'typing' , ( ) => {
58
61
socket . broadcast . emit ( 'typing' , {
59
62
username : socket . username
60
63
} ) ;
61
64
} ) ;
62
65
63
66
// when the client emits 'stop typing', we broadcast it to others
64
- socket . on ( 'stop typing' , function ( ) {
67
+ socket . on ( 'stop typing' , ( ) => {
65
68
socket . broadcast . emit ( 'stop typing' , {
66
69
username : socket . username
67
70
} ) ;
68
71
} ) ;
69
72
70
73
// when the user disconnects.. perform this
71
- socket . on ( 'disconnect' , function ( ) {
74
+ socket . on ( 'disconnect' , ( ) => {
72
75
if ( addedUser ) {
73
76
-- numUsers ;
74
77
0 commit comments