Skip to content

Commit 0b8183b

Browse files
committed
fix reconnection and example
1 parent c402efd commit 0b8183b

5 files changed

Lines changed: 39 additions & 23 deletions

File tree

js/network/onewaydata/changelog.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
* Add possibility to change reconnection time with reconnectionTime
99
* Replaced path option with general purpose condition
1010
* Use the following to keep using path
11+
* Removed a weird condition
12+
* Simplify code with optional chaining
13+
* Fix reconnection
1114

1215
```js
13-
const path = ...
1416
const condition = (request) => {
15-
request.url === path;
17+
return request.url === `/sse`;
1618
};
1719
```

js/network/onewaydata/examples/client.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
<div>
99
<h1>EventSource</h1>
1010

11-
<h2>Last message on default channel</h2>
11+
<h2>Last message without explicit event type</h2>
1212
<p id="lastmessage"></p>
1313

14-
<h2>Goals</h2>
14+
<h2>Message sent with event type football/goal</h2>
1515
<ol id="goals"></ol>
1616
</div>
1717

@@ -31,4 +31,4 @@ <h2>Goals</h2>
3131
});
3232
</script>
3333
</body>
34-
</html>
34+
</html>

js/network/onewaydata/examples/server.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import url from "node:url";
55
import path from "node:path";
66
import {
77
createEventStream,
8+
RECONNECT,
89
sendOne,
9-
defaultChannel,
1010
} from "../source/onewaydata.js";
1111
import {
1212
useDefaultLogging
@@ -22,24 +22,42 @@ const PORT = 8080;
2222
const server = http.createServer((req, res) => {
2323
if (req.url === `/`) {
2424
fs.createReadStream(`${__dirname}/client.html`).pipe(res);
25+
} else {
26+
console.log(req.headers);
2527
}
2628
});
2729

28-
const path = `/sse`;
30+
2931
const condition = (request) => {
30-
request.url === path;
32+
return request.url === `/sse`;
3133
};
3234
const eventStream = createEventStream({ server, condition });
3335
useDefaultLogging({ eventStream });
3436

37+
eventStream.on(RECONNECT, ({lastId, response}) => {
38+
if (lastId) {
39+
// opportunity to resume with sendOne
40+
let dateString = ``;
41+
let numberId = Number(lastId);
42+
if (Number.isFinite(numberId)) {
43+
const d = new Date();
44+
d.setTime(numberId);
45+
dateString = d.toTimeString();
46+
}
47+
sendOne(response, {
48+
event: `football/goal`,
49+
data: `Welcome back, you missed some goals since ${dateString}`,
50+
});
51+
}
52+
});
3553

3654
server.listen(PORT);
3755
console.log(`open http:localhost:${PORT}/`);
3856

3957
let i = 0;
4058
setInterval(() => {
4159
eventStream.send({ data: ++i });
42-
}, 3000);
60+
}, 1000);
4361

4462
setInterval(() => {
4563
eventStream.send({

js/network/onewaydata/readme.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ import {
1919
RECONNECT,
2020
CONNECT,
2121
DISCONNECT,
22-
defaultChannel,
2322
} from "onewaydata";
2423
import { useDefaultLogging } from "onewaydata/source/defaultLogging.js";
2524

2625

2726
const server = ...
28-
const path = `/sse`;
2927
const condition = (request) => {
30-
request.url === path;
28+
return request.url === `/sse`;
3129
};
3230
const eventStream = createEventStream({server, condition, reconnectionTime: 5000 });
3331
useDefaultLogging({ eventStream });
@@ -38,9 +36,9 @@ eventStream.send({ data: `something`, event: `eventName`, id: String(Date.now()
3836
eventStream.on(RECONNECT, ({lastId, response}) => {
3937
if (lastId) {
4038
// opportunity to resume with sendOne
41-
sendOne(response, `here is what you missed since last disconnection`)
39+
sendOne(response, `here is what you missed since last disconnection`);
4240
}
43-
})
41+
});
4442
```
4543

4644
### with express/polka

js/network/onewaydata/source/onewaydata.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ export {
44
RECONNECT,
55
CONNECT,
66
DISCONNECT,
7-
defaultChannel,
87
};
98
import Emitter from "event-e3/event-e3.js";
109

1110

1211
const MIME = `text/event-stream`;
13-
const LAST_ID = `Last-Event-ID`;
14-
const defaultChannel = `message`;
12+
const LAST_ID = (`Last-Event-ID`).toLowerCase(); // node request.headers is lower cased
1513
const HTTP_OK = 200;
1614
const HTTP_PROBLEM = 400;
1715

@@ -47,15 +45,15 @@ const formatEvent = (x) => {
4745
if (event) {
4846
message = `${message}${EVENT}:${event}\n`;
4947
}
50-
if (data && data !== defaultChannel) {
48+
if (data) {
5149
message = `${message}${DATA}:${data}\n`;
5250
}
5351
message = `${message}\n`;
5452
return message;
5553
};
5654

57-
const sendOne = (response, x) => {
58-
const message = formatEvent(x);
55+
const sendOne = (response, messageObject) => {
56+
const message = formatEvent(messageObject);
5957
response.write(message);
6058
};
6159

@@ -64,7 +62,7 @@ const isValidRequestForServerSentEvents = (request) => {
6462
return;
6563
}
6664

67-
if (request.headers.accept && !request.headers.accept.includes(MIME)) {
65+
if (!request.headers?.accept.includes(MIME)) {
6866
return;
6967
}
7068
return true;
@@ -135,8 +133,8 @@ const createEventStream = (options) => {
135133
});
136134
};
137135

138-
const send = (x) => {
139-
const message = formatEvent(x);
136+
const send = (messageObject) => {
137+
const message = formatEvent(messageObject);
140138
responses.forEach(response => {
141139
response.write(message);
142140
});

0 commit comments

Comments
 (0)