2019-09-25 06:39:49 +02:00
|
|
|
# actix-sse
|
2022-03-06 01:43:10 +01:00
|
|
|
|
2019-09-25 06:39:49 +02:00
|
|
|
Example of server-sent events, aka `EventSource`, with actix web.
|
|
|
|
|
|
|
|
```sh
|
2022-02-18 03:22:58 +01:00
|
|
|
cd server-sent-events
|
2019-09-25 06:39:49 +02:00
|
|
|
cargo run
|
|
|
|
```
|
|
|
|
|
2022-02-14 02:16:18 +01:00
|
|
|
Open http://127.0.0.1:8080/ with a browser, then send events with another HTTP client:
|
2019-09-25 06:39:49 +02:00
|
|
|
|
|
|
|
```sh
|
2023-02-08 12:54:57 +01:00
|
|
|
curl -X post 127.0.0.1:8080/broadcast/my_message
|
2019-09-25 06:39:49 +02:00
|
|
|
```
|
|
|
|
|
2022-03-06 01:43:10 +01:00
|
|
|
_my_message_ should appear in the browser with a timestamp.
|
2019-09-25 06:39:49 +02:00
|
|
|
|
|
|
|
## Performance
|
2022-03-06 01:43:10 +01:00
|
|
|
|
2022-02-14 02:37:51 +01:00
|
|
|
This implementation can serve thousands of clients on a 2021 MacBook with no problems.
|
2019-09-25 06:39:49 +02:00
|
|
|
|
2022-02-18 03:13:08 +01:00
|
|
|
Run `node ./benchmark.js` to benchmark your own system:
|
2019-09-25 06:39:49 +02:00
|
|
|
|
|
|
|
```sh
|
|
|
|
$ node benchmark.js
|
2022-02-14 02:16:18 +01:00
|
|
|
Connected: 1000, connection time: 201 ms, total broadcast time: 20 ms^C⏎
|
2019-09-25 06:39:49 +02:00
|
|
|
```
|
|
|
|
|
2022-03-06 01:43:10 +01:00
|
|
|
### Error _Too many open files_
|
|
|
|
|
2019-09-25 06:39:49 +02:00
|
|
|
You may be limited to a maximal number of connections (open file descriptors). Setting maximum number of open file descriptors to 2048:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
ulimit -n 2048
|
|
|
|
```
|
|
|
|
|
2022-02-18 03:13:08 +01:00
|
|
|
Test maximum number of open connections with `node ./drain.js`:
|
2019-09-25 06:39:49 +02:00
|
|
|
|
|
|
|
```sh
|
|
|
|
$ node drain.js
|
2022-02-14 02:16:18 +01:00
|
|
|
Connections dropped: 10450, accepting connections: false^C⏎
|
2019-09-25 06:39:49 +02:00
|
|
|
```
|
|
|
|
|
2022-02-14 02:37:51 +01:00
|
|
|
_Accepting connections_ indicates whether resources for the server have been exhausted.
|