Skip to content

Instantly share code, notes, and snippets.

@nomoney4me
Created November 18, 2018 00:15
Show Gist options
  • Select an option

  • Save nomoney4me/c4d5bfce7c94cf83de2acfc500c13ba9 to your computer and use it in GitHub Desktop.

Select an option

Save nomoney4me/c4d5bfce7c94cf83de2acfc500c13ba9 to your computer and use it in GitHub Desktop.
sse-practice
<!<!DOCTYPE html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>
<div id="data"></div>
<script>
$(document).ready(function() {
var es = new EventSource('/stream')
es.onmessage = function(e) {
$("#data").append(`<li>${e.data}</li>`)
}
es.addEventListener('msg', function(e) {
// console.log(e.)
})
})
</script>
</body>
</html>
const express = require('express')
, app = express()
, SSE = require('express-sse')
, sse = new SSE()
app.use(express.static('./client'))
app.use('/stream', sse.init);
setInterval(() => {
sse.send('hello')
}, 3000)
app.listen(3001, console.log(`listening on 3001`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment