Skip to content

Commit d4454bc

Browse files
committed
SSE return types /2
1 parent 78059a9 commit d4454bc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

aspnetcore/fundamentals/minimal-apis/responses.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,28 @@ In order to document this endpoint correctly the extension method `Produces` is
120120

121121
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_04":::
122122

123-
<a name="binr7"></a>
123+
<a name="binr10"></a>
124124

125125
### Built-in results
126126

127127
[!INCLUDE [results-helpers](~/fundamentals/minimal-apis/includes/results-helpers.md)]
128128

129129
The following sections demonstrate the usage of the common result helpers.
130130

131+
#### Server-Sent Events (SSE)
132+
133+
The [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result.
134+
135+
[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem<T`>`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`.
136+
137+
The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable<SseItem<T>>` that represents the stream of event messages to be sent to the client.
138+
139+
The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client:
140+
141+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_json" :::
142+
143+
For more information, see the [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client.
144+
131145
#### JSON
132146

133147
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_05":::
@@ -150,6 +164,8 @@ The preceding example returns a 500 status code.
150164

151165
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_12":::
152166

167+
#### Problem
168+
153169
#### Text
154170

155171
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08":::

0 commit comments

Comments
 (0)