Skip to content

Support async generator based streaming API in javascript #60585

Open
@davidfowl

Description

@davidfowl

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

JavaScript supports async generators 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<T = unknown, TReturn = any, TNext = any>
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).

+ public async *streamAsync<T = any>(methodName: string, ...args: any[]): AsyncGenerator<T, void, void>;
public stream<T = any>(methodName: string, ...args: any[]): IStreamResult<T>;

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:

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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviewsarea-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions