Support async generator based streaming API in javascript
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Is your feature request related to a problem? Please describe the problem.
JavaScript supports [async generators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator) now and SignalR still only supports the RX based callback model:
https://learn.microsoft.com/en-us/aspnet/core/signalr/streaming?view=aspnetcore-9.0
## Proposed API
`AsyncGenerator`
Using `TReturn = void` and `TNext = void` means if you manually write the stream loop you can't inject your own values into the generator via `stream.next(someValue)` and can't complete the stream with a random value via `stream.return(someValue)`.
```diff
+ public async *streamAsync(methodName: string, ...args: any[]): AsyncGenerator;
public stream(methodName: string, ...args: any[]): IStreamResult;
```
### Describe the solution you'd like
https://learn.microsoft.com/en-us/aspnet/core/signalr/streaming?view=aspnetcore-9.0#javascript-client
An API that looks like this:
```javascript
var stream = await connection.streamAsync("Counter", 10, 500);
try {
for await (const value of stream) {
var li = document.createElement("li");
li.textContent = value;
document.getElementById("messagesList").appendChild(li);
}
} catch (err) {
var li = document.createElement("li");
li.textContent = err;
document.getElementById("messagesList").appendChild(li);
}
var li = document.createElement("li");
li.textContent = "Stream completed";
document.getElementById("messagesList").appendChild(li);
```
Canceling stream:
```javascript
var stream = await connection.streamAsync("Counter", 10, 500);
var done = false;
setTimeout(function () {
done = true;
}, 3000);
for await (const value of stream) {
if (done) {
stream.return();
}
}
```
### Additional context
_No response_
Contributor guide
Research direction
Start by reading the existing RX-based connection.stream API and the JavaScript-client streaming guidance linked in the issue. Done means exposing the proposed streamAsync(methodName, ...args) async-generator behavior, including iteration, errors, and cancellation, with corresponding JavaScript-client test coverage.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100