forked from markbrown4/server-sent-events-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
53 lines (41 loc) · 1.28 KB
/
server.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
var http = require('http');
var express = require('express');
var SSE = require('sse');
var app = express().use(express.static('public'));
var server = http.createServer(app);
var clients = [];
server.listen(8080, '127.0.0.1', function() {
var sse = new SSE(server);
sse.on('connection', function(stream) {
clients.push(stream);
console.log('Opened connection 🎉');
var json = JSON.stringify({ message: 'Gotcha' });
stream.send(json);
console.log('Sent: ' + json);
stream.on('close', function() {
clients.splice(clients.indexOf(stream), 1);
console.log('Closed connection 😱');
});
});
});
var broadcast = function() {
var json = JSON.stringify({ message: 'Hello hello!' });
clients.forEach(function(stream) {
stream.send(json);
console.log('Sent: ' + json);
});
}
setInterval(broadcast, 3000)
// can receive from the client with standard http and broadcast
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.post('/api', function(req, res) {
var message = JSON.stringify(req.body);
console.log('Received: ' + message);
res.status(200).end();
var json = JSON.stringify({ message: 'Something changed' });
clients.forEach(function(stream) {
stream.send(json);
console.log('Sent: ' + json);
});
})