-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (24 loc) · 1007 Bytes
/
index.js
File metadata and controls
30 lines (24 loc) · 1007 Bytes
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
var express = require('express');
var socket = require('socket.io');
var moment = require('moment');
// App setup
var app = express();
var server = app.listen(process.env.PORT || 4000, function() {
console.log("Listening to requests on port 4000.");
});
// Static files (this basically means look in the "public" folder to run the files)
app.use(express.static('public'));
// Socket setup (pass in the server created earlier)
var io = socket(server);
// socket parameter refers to the individual socket between client and server. can be many instances at once.
io.on('connection', function(socket) {
console.log("Made socket connection", socket.id);
//if a chat is received, send it to ALL listening sockets
socket.on("chat", function(data) {
io.sockets.emit("chat", data);
});
//if the user is typing, broadcast to all other listening sockets (that aren't the user's)
socket.on("typing", function(data) {
socket.broadcast.emit("typing", data);
});
});