Skip to content

Instantly share code, notes, and snippets.

@colinbes
Created January 6, 2020 15:35
Show Gist options
  • Save colinbes/bf524170885380891620dcf368923c33 to your computer and use it in GitHub Desktop.
Save colinbes/bf524170885380891620dcf368923c33 to your computer and use it in GitHub Desktop.
Vuejs file for EventSource
const app = new Vue({
el: '#app',
data: {
message: 'Vue/SSE Example!',
now: 'wait for it ...'
},
created () {
this.setupStream()
},
methods: {
setup () {
this.now = 'yup'
},
setupStream () {
let evtSource = new EventSource("http://localhost:8082/events")
evtSource.addEventListener('myEvent', event => {
let data = JSON.parse(event.data)
this.now = data.msg
}, false)
evtSource.addEventListener('error', event => {
if (event.readyState == EventSource.CLOSED) {
console.log('Event was closed');
console.log(EventSource);
}
}, false);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment