-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.js
112 lines (89 loc) · 3.58 KB
/
slack.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require('dotenv').config()
const express = require('express');
const socketio = require('socket.io');
const models = require('./models');
const User = require('./models/Users');
const debug = require('debug')('chat')
const app = express();
const mongoose = require('mongoose');
const { handleChatConnection } = require('./socket');
const flash = require("express-flash");
const passport = require("passport");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const mainRoutes = require("./routes/main");
const roomsRoutes = require("./routes/rooms");
const connectDB = require('./config/database');
// Passport config
require("./config/passport")(passport);
connectDB();
//Using EJS for views
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/public'));
//Body Parsing
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
//Use flash messages for errors, info, ect...
app.use(flash());
const expressServer = app.listen(process.env.PORT || 8080);
// any time we listen to events with io, we are listening for events coming from all clients
const io = socketio(expressServer);
const sessionMiddleware = session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DB_CONNECTION_STRING }),
});
// Setup Sessions - stored in MongoDB
app.use(sessionMiddleware);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// convert a connect middleware to a Socket.IO middleware
const wrap = middleware => (socket, next) => middleware(socket.request, {}, next);
io.use(wrap(sessionMiddleware));
// Share io instance throughout app files
app.use(function (req, res, next) {
req.io = io;
next();
});
//Setup Routes For Which The Server Is Listening
app.use("/", mainRoutes);
app.use("/chat", roomsRoutes);
// loop through each ns and listen for a connection
models.Namespaces.find()
.exec()
.then((namespaces) => {
// note that io.on === io.of('/').on ;)
io.on('connection', (socket) => {
console.log(socket.request.session);
// build an array of namespaces with img and endpoint to send back
// with this namespace map, every connection gets the same list of namespaces
let nsData = namespaces.filter((ns) => ns.name === process.env.DEFAULT_NAMESPACE).map((ns) => {
return {
img: ns.img,
endpoint: ns.endpoint
};
});
// We need to use socket, not IO this is
// because we want it to go to just this client
socket.emit('nsList', nsData); // send nsData back to the client
socket.on('disconnect', function () {
console.log('user disconnected');
});
})
//Set up connection handler for each namespace
namespaces.filter((ns) => ns.name === process.env.DEFAULT_NAMESPACE).forEach((namespace) => {
let nsp = io.of(namespace.endpoint);
nsp.on('connection', (socket) => {
handleChatConnection(socket, nsp, namespace);
})
})
})
// This is outdated now -- find another way to emit user counts to all clients
// function updateUsersInRoom(namespace, roomToJoin) {
// // send back the number of users in this room to ALL sockets connected to this room
// io.of(namespace.endpoint).in(roomToJoin).clients((error, clients) => {
// io.of(namespace.endpoint).in(roomToJoin).emit('updateMembers', clients.length)
// })
// }