Skip to content

Instantly share code, notes, and snippets.

@rrmdn
Created March 4, 2016 18:28
Show Gist options
  • Select an option

  • Save rrmdn/b213938e9f722ccbd47a to your computer and use it in GitHub Desktop.

Select an option

Save rrmdn/b213938e9f722ccbd47a to your computer and use it in GitHub Desktop.
import express from 'express';
import SSE from 'sse';
let app = express();
app.use('/', (req, res) => {
res.status(200).send('');
});
app.listen(3000, (err) => {
if(err) throw err;
let sse = new SSE(app);
// jika ada koneksi klien
sse.on('connection', (client) => {
let isActive = true;
// client bisa emit data, client.send('string')
const sendInterval = () => {
setTimeout(() => {
client.send(JSON.stringify({message: 'halooo 😜'}));
if(isActive) sendInterval();
}, 2000)
}
client.on('close', () => {
// stop interval
isActive = false;
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment