Skip to content

Commit c7077be

Browse files
committed
Adding documentation around disconnecting clients (#39)
1 parent 8db24c0 commit c7077be

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

DocFx.AspNetCore.ServerSentEvents/DocFx.AspNetCore.ServerSentEvents.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp3.1</TargetFramework>
3+
<TargetFramework>net5.0</TargetFramework>
44
</PropertyGroup>
55
<ItemGroup>
66
<None Remove="log.txt" />
@@ -9,7 +9,7 @@
99
<Folder Include="wwwroot\" />
1010
</ItemGroup>
1111
<ItemGroup>
12-
<PackageReference Include="docfx.console" Version="2.48.0">
12+
<PackageReference Include="docfx.console" Version="2.58.8">
1313
<PrivateAssets>all</PrivateAssets>
1414
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1515
</PackageReference>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Disconneting Clients
2+
3+
There is an option to disconnect a client from server. The flow is based on responding with *204 No Content* status code to reconnect attempt.
4+
5+
<center>![Server-Sent Events Client Disconnect Flow Diagram](../resources/disconneting-client-flow.png)</center>
6+
7+
## Prerequisites
8+
9+
The client disconnect functionality requires registering two services, which are not registered by default.
10+
11+
### Client Identifier Provider
12+
13+
First is implementation of [`IServerSentEventsClientIdProvider`](../api/Lib.AspNetCore.ServerSentEvents.IServerSentEventsClientIdProvider.html), which responsibility is to provide an identifier for a client. This identifier should remain the same if client performs reconnect(s). There is no ready to use implementation of this provider, as the approach to this should strongly depend on context. Below is a sample cookie based implementation (which is certainly not sophisticated enough for production scenarios).
14+
15+
```cs
16+
internal class CookieBasedServerSentEventsClientIdProvider : IServerSentEventsClientIdProvider
17+
{
18+
private const string COOKIE_NAME = ".ServerSentEvents.Guid";
19+
20+
public Guid AcquireClientId(HttpContext context)
21+
{
22+
Guid clientId;
23+
24+
string cookieValue = context.Request.Cookies[COOKIE_NAME];
25+
if (String.IsNullOrWhiteSpace(cookieValue) || !Guid.TryParse(cookieValue, out clientId))
26+
{
27+
clientId = Guid.NewGuid();
28+
29+
context.Response.Cookies.Append(COOKIE_NAME, clientId.ToString());
30+
}
31+
32+
return clientId;
33+
}
34+
35+
public void ReleaseClientId(Guid clientId, HttpContext context)
36+
{
37+
context.Response.Cookies.Delete(COOKIE_NAME);
38+
}
39+
}
40+
```
41+
42+
There is a helper method which simplifies registering an implementation.
43+
44+
```cs
45+
public class Startup
46+
{
47+
...
48+
49+
public void ConfigureServices(IServiceCollection services)
50+
{
51+
services.AddServerSentEvents();
52+
53+
// Register cookie based clients identifier provider for Server Sent Events
54+
services.AddServerSentEventsClientIdProvider<CookieBasedServerSentEventsClientIdProvider>();
55+
56+
...
57+
}
58+
59+
...
60+
}
61+
```
62+
63+
### "No Reconnect" Identifiers Store
64+
65+
Second is implementation of [`IServerSentEventsNoReconnectClientsIdsStore`](../api/Lib.AspNetCore.ServerSentEvents.IServerSentEventsNoReconnectClientsIdsStore.html), which responsibility is to store identifiers which should not be allowed to reconnect. There are two ready to use implementations. First stores the identifiers in memory, while the second is backed by distributed cache. Both have ready to use methods to register them.
66+
67+
```cs
68+
public class Startup
69+
{
70+
...
71+
72+
public void ConfigureServices(IServiceCollection services)
73+
{
74+
services.AddServerSentEvents();
75+
76+
// Register cookie based clients identifier provider for Server Sent Events
77+
services.AddServerSentEventsClientIdProvider<CookieBasedServerSentEventsClientIdProvider>();
78+
79+
// Register IServerSentEventsNoReconnectClientsIdsStore backed by memory store.
80+
services.AddInMemoryServerSentEventsNoReconnectClientsIdsStore();
81+
82+
...
83+
}
84+
85+
...
86+
}
87+
```
88+
89+
## Disconnecting a Client
90+
91+
Disconnecting a client is as simple as calling `Disconnect()` on a [`IServerSentEventsClient`](../api/Lib.AspNetCore.ServerSentEvents.IServerSentEventsClient.html).

DocFx.AspNetCore.ServerSentEvents/docfx.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"articles/getting-started.md",
2727
"articles/authorization.md",
2828
"articles/groups.md",
29+
"articles/disconneting-clients.md",
2930
"articles/advanced.md"
3031
]
3132
}
@@ -34,7 +35,8 @@
3435
{
3536
"files": [
3637
"resources/svg/logo.svg",
37-
"resources/ico/favicon.ico"
38+
"resources/ico/favicon.ico",
39+
"resources/disconneting-client-flow.png"
3840
]
3941
}
4042
],

DocFx.AspNetCore.ServerSentEvents/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Lib.AspNetCore.ServerSentEvents
2+
[![NuGet Version](https://img.shields.io/nuget/v/Lib.AspNetCore.ServerSentEvents?label=Lib.AspNetCore.ServerSentEvents&logo=nuget)](https://www.nuget.org/packages/Lib.AspNetCore.ServerSentEvents)
3+
[![NuGet Downloads](https://img.shields.io/nuget/dt/Lib.AspNetCore.ServerSentEvents?label=⭳)](https://www.nuget.org/packages/Lib.AspNetCore.ServerSentEvents)
24

35
Lib.AspNetCore.ServerSentEvents is a library which provides Server-Sent Events (SSE) support for ASP.NET Core.
46

@@ -24,8 +26,8 @@ There are some blog posts available which describe implementation details and so
2426
- [Server-Sent Events (or WebSockets) broadcasting in load balancing scenario with Redis](https://www.tpeczek.com/2017/09/server-sent-events-or-websockets.html)
2527
- [Server-Sent Events and ASP.NET Core - You may need keep alives](https://www.tpeczek.com/2018/08/server-sent-events-and-aspnet-core-you_9.html)
2628

27-
## Donating
29+
## Sponsor this project
2830

29-
My blog and open source projects are result of my passion for software development, but they require a fair amount of my personal time. If you got value from any of the content I create, then I would appreciate your support by [buying me a coffee](https://www.buymeacoffee.com/tpeczek).
31+
My blog and open source projects are result of my passion for software development, but they require a fair amount of my personal time. If you got value from any of the content I create, then I would appreciate your support by sponsoring me.
3032

31-
<a href="https://www.buymeacoffee.com/tpeczek"><img src="https://www.buymeacoffee.com/assets/img/custom_images/black_img.png" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" target="_blank"></a>
33+
<iframe src="https://github.com/sponsors/tpeczek/button" title="Sponsor tpeczek" height="35" width="116" style="border: 0;"></iframe>
16.1 KB
Loading

DocFx.AspNetCore.ServerSentEvents/toc.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
# [Groups](articles/groups.md)
88

9+
# [Disconneting Clients](articles/disconneting-clients.md)
10+
911
# [Advanced](articles/advanced.md)
1012

1113
# [API Reference](api/Lib.AspNetCore.ServerSentEvents.html)

0 commit comments

Comments
 (0)