Created
March 4, 2016 18:28
-
-
Save rrmdn/b213938e9f722ccbd47a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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