-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch from WebSocket to EventSource (#34)
BREAKING CHANGE: /ws endpoint changed to /sse
- Loading branch information
Showing
10 changed files
with
1,615 additions
and
1,457 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
export default { | ||
filters: { | ||
capitalize(value) { | ||
capitalize (value) { | ||
if (!value) { | ||
return '' | ||
} | ||
|
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export default { | ||
methods: { | ||
logSse (...args) { | ||
this.log('[SSE]', ...args) | ||
}, | ||
|
||
logSseError (...args) { | ||
this.logError('[SSE]', ...args) | ||
}, | ||
|
||
sseConnect (path) { | ||
this.logSse(`Connecting to ${path}...`) | ||
|
||
this.$sse = new EventSource(path) | ||
this.$sse.addEventListener('message', event => this.onSseMessage(event)) | ||
}, | ||
|
||
onSseMessage (message) { | ||
const data = JSON.parse(message.data) | ||
|
||
data && this.onSseData && this.onSseData(data) | ||
}, | ||
|
||
sseClose () { | ||
if (this.$sse) { | ||
this.$sse.close() | ||
delete this.$sse | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const { status, header } = require('node-res') | ||
|
||
class SSE { | ||
constructor () { | ||
this.subscriptions = new Set() | ||
this.counter = 0 | ||
} | ||
|
||
// Subscribe to a channel and set initial headers | ||
subscribe (req, res) { | ||
req.socket.setTimeout(0) | ||
|
||
status(res, 200) | ||
header(res, 'Content-Type', 'text/event-stream') | ||
header(res, 'Cache-Control', 'no-cache') | ||
header(res, 'Connection', 'keep-alive') | ||
|
||
this.subscriptions.add(res) | ||
res.on('close', () => this.subscriptions.delete(res)) | ||
this.broadcast('ready', {}) | ||
} | ||
|
||
// Publish event and data to all connected clients | ||
broadcast (event, data) { | ||
this.counter++ | ||
// Do console.log(this.subscriptions.size) to see, if there are any memory leaks | ||
for (const res of this.subscriptions) { | ||
this.clientBroadcast(res, event, data) | ||
} | ||
} | ||
|
||
// Publish event and data to a given response object | ||
clientBroadcast (res, event, data) { | ||
res.write(`id: ${this.counter}\n`) | ||
res.write(`event: message\n`) | ||
res.write(`data: ${JSON.stringify({ event, ...data })}\n\n`) | ||
} | ||
} | ||
|
||
module.exports = SSE |
Oops, something went wrong.