-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (26 loc) · 886 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const express = require('express');
const app = express();
const server = require('http').Server(app);
const { createAdapter } = require("@socket.io/redis-adapter");
const { createClient } = require("redis");
const io = require('socket.io')(server);
const pubClient = createClient({ url: process.env.REDIS_URL });
pubClient.connect().then(() => {
console.log(`connected.`)
})
const subClient = pubClient.duplicate();
io.adapter(createAdapter(pubClient, subClient));
var your_namespace_socket = io.of('/');
your_namespace_socket.on('connection', function(socket) {
socket.on('join', function(room){
socket.join(room);
});
socket.on('room1', function(msg) {
io.to('room1').emit('chat message', msg);
})
});
app.use(express.static('public'));
const port = process.env.PORT || 3000
server.listen(port, function(){
console.log(`listening on *:${port}`);
});