Skip to content

Instantly share code, notes, and snippets.

View iamparnab's full-sized avatar
🖤
Programming

Parnab Sanyal iamparnab

🖤
Programming
  • Walmart Global Tech India
  • Bangalore
View GitHub Profile
@iamparnab
iamparnab / request.txt
Last active June 7, 2022 02:05
A raw HTTP Request
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
@iamparnab
iamparnab / SSE_fields.js
Created November 7, 2020 14:12
Sending different fields in SSE response body
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
@iamparnab
iamparnab / custom_event_listener_sse.js
Last active November 7, 2020 14:16
Listen to custom events in SSE
const ev = new EventSource("url goes here");
ev.addEventListener('sampleEvent', message => {
console.log(message.data); // the response will be available under data field
})
@iamparnab
iamparnab / SSE_public_script.js
Created November 7, 2020 13:34
Connect to SSE endpoint from JavaScript
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();
}
@iamparnab
iamparnab / handlers_index.js
Last active November 7, 2020 13:04
SSE handler
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`