-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (29 loc) · 868 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'),
cors = require('cors'),
socket = require('socket.io');
// App setup
const app = express();
//enable all cors
app.use(cors());
//start the server
const server = app.listen(4000, function(){
console.log('listening to requests on port 4000');
});
// Static files
app.use(express.static('public'));
// Socket setup
const io = socket(server, {
//set allowed origins
cors: {origin: ["http://localhost:3000"]}
});
io.on('connection', function(socket){
console.log('Made socket connection', socket.id);
socket.on('chat', function(data){
//send data to all connected clients including sender
io.sockets.emit('chat', data);
});
socket.on('typing', function(data) {
//send data to all connected clients except sender
socket.broadcast.emit('typing', data);
});
});