-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
DocFx.AspNetCore.ServerSentEvents/articles/getting-started.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Getting Started | ||
|
||
## Configuration | ||
|
||
In order to add the Server-Sent Events (SSE) support to an application a required service and middleware must be registered. The library provides [extensions](../api/Lib.AspNetCore.ServerSentEvents.ServerSentEventsMiddlewareExtensions.html) which make it really simple. | ||
|
||
```cs | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
... | ||
|
||
services.AddServerSentEvents(); | ||
|
||
... | ||
} | ||
|
||
public void Configure(IApplicationBuilder app) | ||
{ | ||
... | ||
|
||
app.MapServerSentEvents("/default-sse-endpoint"); | ||
|
||
... | ||
} | ||
} | ||
``` | ||
|
||
The `AddServerSendEvents()` method registers default service which provides operations over Server-Sent Events protocol, while `MapServerSentEvents()` maps this service to a specific endpoint. The `MapServerSentEvents()` method can be called multiple times for different endpoints but all of them will be mapped to same service which will make them indistinguishable from application perspective. In order to create distinguishable endpoints a delivered version of [`ServerSentEventsService`](../api/Lib.AspNetCore.ServerSentEvents.ServerSentEventsService.html) should be registered (it doesn't need to override any of the default service functionality). Below is an example of such configuration. | ||
|
||
```cs | ||
internal interface INotificationsServerSentEventsService : IServerSentEventsService | ||
{ } | ||
|
||
internal class NotificationsServerSentEventsService : ServerSentEventsService, INotificationsServerSentEventsService | ||
{ } | ||
|
||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
... | ||
|
||
services.AddServerSentEvents(); | ||
services.AddServerSentEvents<INotificationsServerSentEventsService, NotificationsServerSentEventsService>(); | ||
|
||
... | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider) | ||
{ | ||
... | ||
|
||
app.MapServerSentEvents("/default-sse-endpoint"); | ||
app.MapServerSentEvents("/notifications-sse-endpoint", serviceProvider.GetService<NotificationsServerSentEventsService>()); | ||
|
||
... | ||
} | ||
} | ||
``` | ||
|
||
## Sending Messages | ||
|
||
In order to send a message to an endpoint the instance of the service registered for that endpoint should be obtained. The easiest way is to rely on dependency injection. | ||
|
||
```cs | ||
public class NotificationsController : Controller | ||
{ | ||
private readonly INotificationsServerSentEventsService _serverSentEventsService; | ||
|
||
public NotificationsController(INotificationsServerSentEventsService serverSentEventsService) | ||
{ | ||
_serverSentEventsService = serverSentEventsService; | ||
} | ||
|
||
... | ||
} | ||
``` | ||
|
||
### Sending to all clients | ||
|
||
Sending message to all clients is as simple as calling `IServerSentEventsService.SendEventAsync`. Under the hood the service will take care of distributing the message to all connected clients. | ||
|
||
### Sending to specific client | ||
|
||
The `IServerSentEventsService.GetClients()` method returns list of all currently connected clients. Every client (represented by [`IServerSentEventsClient`](../api/Lib.AspNetCore.ServerSentEvents.IServerSentEventsClient.html)) exposes `User` property and `SendEventAsync` method. The `User` property contains a `System.Security.Claims.ClaimsPrincipal` which can be used to filter a specific client. Additionally there is an `Id` property available which value can be stored and later passed to `IServerSentEventsService.GetClient()` in order to retrieve a specific client. | ||
|
||
## Handling Auto Reconnect | ||
|
||
Server-Sent Events provide auto reconnect and tracking of the last seen message functionality. If the connection is dropped, client will automatically reconnect to the server and optionally advertise the identifier of the last seen message. This allows for retransmission of lost messages. In order to use this mechanism the `OnReconnectAsync` method must be overridden when delivering from `ServerSentEventsService`. When client reconnects the method will be called with `IServerSentEventsClient` representing the client and the identifier of last message which client has received. | ||
|
||
### Changing Reconnect Interval | ||
|
||
The interval after which client attempts to reconnect can be controlled by the application. In order to change the interval for specific endpoint it is enough to call `IServerSentEventsService.ChangeReconnectIntervalAsync`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,8 @@ | |
"files": [ | ||
"api/*.yml", | ||
"toc.md", | ||
"index.md" | ||
"index.md", | ||
"articles/getting-started.md" | ||
] | ||
} | ||
], | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
# [Introduction](index.md) | ||
|
||
# [Getting Started](articles/getting-started.md) | ||
|
||
# [API Reference](api/Lib.AspNetCore.ServerSentEvents.html) |