-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
36 lines (27 loc) · 762 Bytes
/
app.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
const express = require("express");
const socket = require("socket.io");
// App Setup
const app = express();
const port = process.env.PORT || 5000;
const server = app.listen(port, () =>
console.log(`Server started on port ${port}`)
);
// Static files
app.use(express.static("public"));
// Socket setup
const io = socket(server);
io.on("connection", function(socket) {
console.log(`Socket.io connected to server`, socket.id);
// Handle chat event
socket.on("chat", function(data) {
io.sockets.emit("chat", data);
});
// Typing event
socket.on("typing", function(data) {
socket.broadcast.emit("typing", data);
});
// Member joined event
socket.on("joined", function(data) {
socket.broadcast.emit("joined", data);
});
});