Arebis.Core.AspNet.ServerSentEvents.EntityFramework 8.0.1

dotnet add package Arebis.Core.AspNet.ServerSentEvents.EntityFramework --version 8.0.1                
NuGet\Install-Package Arebis.Core.AspNet.ServerSentEvents.EntityFramework -Version 8.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Arebis.Core.AspNet.ServerSentEvents.EntityFramework" Version="8.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Arebis.Core.AspNet.ServerSentEvents.EntityFramework --version 8.0.1                
#r "nuget: Arebis.Core.AspNet.ServerSentEvents.EntityFramework, 8.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Arebis.Core.AspNet.ServerSentEvents.EntityFramework as a Cake Addin
#addin nuget:?package=Arebis.Core.AspNet.ServerSentEvents.EntityFramework&version=8.0.1

// Install Arebis.Core.AspNet.ServerSentEvents.EntityFramework as a Cake Tool
#tool nuget:?package=Arebis.Core.AspNet.ServerSentEvents.EntityFramework&version=8.0.1                

Arebis.Core.AspNet.ServerSentEvents.EntityFramework

Introduction

The Arebis.Core.AspNet.ServerSentEvents.EntityFramework component provides an EntityFramework store for ServerSent Events support for ASP.NET applications.

This store offers the ability for browser clients to reconnect to different instances of a webfarm or webgarden and still catch up missed events.

Setup

1. Define client data

Create a class that inherits from ServerSentEventsClientData to hold client-specific data. This data can be used to filter recipient clients when dispatching events.

For instance:

public class MySseClientData : ServerSentEventsClientData
{
    public string? Market { get; internal set; }
}

See Arebis.Core.AspNet.ServerSentEvents for more details.

2. Define a, EntityFramework client data store

Define where to store client data as well as the DbContext to use and it's connection string:

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")!;
builder.Services.AddDbContext<EfServerSentEventsDbContext<MySseClientData>>(options => options.UseSqlServer(connectionString));
builder.Services.AddSingleton<IServerSentEventsClientsDataStore<MySseClientData>, EfServerSentEventsClientsDataStore<MySseClientData>>();

3. Create database schema

Create the required database schema. For SQL Server, the following script can be used:

IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = 'sse')
BEGIN
   EXECUTE ('CREATE SCHEMA [sse] AUTHORIZATION [dbo]')
END
GO

CREATE TABLE [sse].[ClientData]
(
    [Identifier] UNIQUEIDENTIFIER NOT NULL,
	[Path] NVARCHAR(2000) NOT NULL,
	[LastUsedId] INT NOT NULL,
	[LastEventQueuedTime] DATETIME2 NULL,
	CONSTRAINT [PK_ClientData] PRIMARY KEY CLUSTERED ([Identifier] ASC)
) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_ClientData_LastEventQueuedTime] ON [sse].[ClientData] ([LastEventQueuedTime] ASC)
GO

CREATE TABLE [sse].[ClientDataEvents]
(
    [ClientIdentifier] UNIQUEIDENTIFIER NOT NULL,
	[Id] INT NOT NULL,
	[Type] NVARCHAR(200) NOT NULL,
	[Data] NVARCHAR(MAX) NOT NULL,
	[ExpiryTime] DATETIME2 NULL,
	[IsSent] BIT NOT NULL CONSTRAINT [DF_ClientData_IsSet] DEFAULT 0,
	CONSTRAINT [PK_ClientDataEvents] PRIMARY KEY CLUSTERED ([ClientIdentifier] ASC, [Id] ASC)
) ON [PRIMARY]
GO

Extend the schema with the client-sepcific data, for instance:

ALTER TABLE [sse].[ClientData]
ADD [Market] NVARCHAR(200) NULL
GO

Consider adding indexes for properties that are used in bulk event send queries.

4. Further setup Server-Sent Events

Proceed with step 3 of the instructions to setup the Arebis.Core.AspNet.ServerSentEvents component.

Further Considerations

Data management

Data is currently not deleted from the database. Consider adding a scheduled job that regularly deletes deprectated clients and events. For instance using the following SQL commands:

DELETE FROM [sse].[ClientDataEvents] WHERE [IsSent] = 1 OR [ExpiryTime] < GETUTCDATE()
GO

DELETE FROM [sse].[ClientData] WHERE DATEADD(hh, 24, [LastEventQueuedTime]) < GETUTCDATE()
GO
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.1 73 12/2/2024
8.0.0 79 11/29/2024