cloudflare / cloudflare/workers-rs

[Server Side Events (SSE)] Support for server side events

Open
#350 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
3.7k
Forks
429
Avg merge
20h 28m
Merged PRs (30d)
7

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Description
Straight forward impl.. I see in Postman required headers and Connection opened/ Connection closed. But body is always empty and there are no messages send. What I am doing wrong? Does Cloud Flare have SSE support?

I tried to implement SSE using stream. I thought, it would be easy:
```
get_async("/sse", |_, _| async {
let mut stream = futures_util::stream::repeat(())
.take(10)
.enumerate()
.then(|(index, _)| async move {
console_log!("Sending {}", index);
let event = json!({
"message": "Hello from the server!"
});
let event_json = serde_json::to_string(&event).unwrap();
let message = json!({ "data": event_json });
Delay::from(Duration::from_millis(100)).await;
Ok::(message.to_string())
});

let mut resp = Response::from_stream(stream)?;

resp.headers_mut().set("Content-Type", "text/event-stream")?;
resp.headers_mut().set("Cache-Control", "no-cache")?;
resp.headers_mut().set("Connection", "keep-alive")?;

Ok(resp)
})
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.