-
-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
with-server-sent-events
example (#204)
* docs: add server-sent events example * fix: use sirv instead of serve-static and use single quotes * update comment * update html * Update readme.md --------- Co-authored-by: Luke Edwards <[email protected]>
- Loading branch information
1 parent
58cb4f7
commit a72e13c
Showing
4 changed files
with
90 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const { join } = require('path'); | ||
const polka = require('polka'); | ||
|
||
const { PORT = 3000 } = process.env; | ||
const dir = join(__dirname, 'public'); | ||
const assets = require('sirv')(dir); | ||
|
||
polka() | ||
.use(assets) | ||
.get('/subscribe', (request, response) => { | ||
// You should add 'access-control-allow-origin' for | ||
// cross-origin requests. We don't here because localhost | ||
response.writeHead(200, { | ||
'Content-Type': 'text/event-stream', | ||
'Cache-Control': 'no-cache', | ||
'Connection': 'keep-alive', | ||
}); | ||
|
||
setInterval(() => { | ||
response.write('data: ' + Date.now() + '\n\n'); | ||
}, 1e3); | ||
|
||
request.on('close', () => { | ||
response.end(); | ||
}); | ||
}) | ||
.listen(PORT, () => { | ||
console.log(`> Running on http://localhost:${PORT}`); | ||
}); |
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,9 @@ | ||
{ | ||
"scripts": { | ||
"start": "node index" | ||
}, | ||
"dependencies": { | ||
"polka": "latest", | ||
"sirv": "latest" | ||
} | ||
} |
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,33 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Polka | Server Sent Events</title> | ||
</head> | ||
<body> | ||
<h1>Server Sent Events</h1> | ||
<ul id="sse-list"></ul> | ||
|
||
<script> | ||
document.addEventListener('DOMContentLoaded', function () { | ||
const list = document.getElementById('sse-list'); | ||
const src = new EventSource('/subscribe'); | ||
|
||
src.onopen = function () { | ||
console.log('[sse] connected'); | ||
}; | ||
|
||
src.onmessage = function (event) { | ||
console.log('[sse] message', event); | ||
const li = document.createElement('li'); | ||
li.innerText = `server time: ${event.data}`; | ||
list.appendChild(li); | ||
}; | ||
|
||
src.onerror = function () { | ||
console.log('[sse]: error!'); | ||
}; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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,19 @@ | ||
# Example: Server-Sent Events | ||
|
||
Small `polka` example demonstrating how to use [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events). | ||
|
||
The `public` directory contains an `index.html` file managing the Event Source Web API to subscribe to Server-Sent Events. | ||
This file is served by Polka as a static asset using [`sirv`](https://github.com/lukeed/sirv) middleware. | ||
|
||
On the server-side, the `/subscribe` endpoint initiates the communication channel for events to be sent. | ||
|
||
## Setup | ||
|
||
```sh | ||
$ npm install | ||
$ npm start | ||
``` | ||
|
||
## Usage | ||
|
||
Open a browser to `localhost:3000` and Server-Sent Events will be automatically subscribed to. |