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
GET /todos/1 HTTP/3 | |
Host: jsonplaceholder.typicode.com | |
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0 | |
Accept: */* | |
Accept-Language: en-US,en;q=0.5 | |
Accept-Encoding: gzip, deflate, br | |
Referer: https://jsonplaceholder.typicode.com/ | |
Alt-Used: jsonplaceholder.typicode.com | |
Connection: keep-alive | |
Cookie: _ga_E3C3GCQVBN=GS1.1.1654567417.1.0.1654567417.0; _ga=GA1.1.2029341261.1654567418 |
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
response.write('data: First message\n'); | |
response.write('event: sampleEvent\n'); | |
response.write('id: 1\n'); | |
response.write('retry: 1000\n'); | |
response.write('\n\n'); // flush the message | |
response.write('data: Second message\n'); // start a new message | |
response.write('event: sampleEvent\n'); | |
response.write('id: 2\n'); | |
response.write('\n\n'); // flush the message |
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
const ev = new EventSource("url goes here"); | |
ev.addEventListener('sampleEvent', message => { | |
console.log(message.data); // the response will be available under data field | |
}) |
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
function connect(path) { | |
const event = new EventSource(BASE_URL + path); | |
event.onopen = () => console.log("Opened"); | |
event.onmessage = function (ev) { | |
const news = JSON.parse(ev.data); | |
render("#data", news.done ? "No more updates available." : news.value); | |
if (news.done) { | |
event.close(); | |
markEnd(); | |
} |
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
function sseHandler(res) { | |
const mySubscription = ChannelFactNews(); | |
res.writeHead(200, { | |
"Content-Type": "text/event-stream", | |
}); | |
const timer = setInterval(() => { | |
const nextValue = mySubscription.next(); | |
res.write( | |
`data: {"done":${nextValue.done}, "value": "${nextValue.value}"}\n\n` |