-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (34 loc) · 942 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {fetchEventSource} from '@microsoft/fetch-event-source';
const serverBaseURL = "http://localhost:5000";
const fetchData = async () => {
await fetchEventSource(`${serverBaseURL}/sse`, {
method: "POST",
headers: {
Accept: "text/event-stream",
},
fetch: fetch,
onopen(res) {
console.log('on open');
if (res.ok && res.status === 200) {
console.log("Connection made ", res);
} else if (
res.status >= 400 &&
res.status < 500 &&
res.status !== 429
) {
console.log("Client side error ", res);
}
},
onmessage(event) {
console.log("new message");
console.log(event.data);
},
onclose() {
console.log("Connection closed by the server");
},
onerror(err) {
console.log("There was an error from server", err);
},
});
};
fetchData();