Skip to content

sixpaq/AspNetCore.Realtime

Repository files navigation

AspNetCore.Realtime

AspNetCore.Realtime is a module, which provides realtime updates on data changes. Modified data in any database is automatically published the updates to a web client.

It is a publish and subscribe system between client (Javascript) and server (AspNetCore). It vastly simplifies the client programming.

This system is based on the same publish and subscribe system that MeteorJS uses. The client side has a mini-mongo database that contains the data that you are subscibed to. All updates to the local database are automatically synchronized with the server and changes at the server side are synchronized with everyone who's subscribed to the data.

Advantages of this approach are:

  • Less programming logic at the client side, because data is automatically updated
  • Less data traffic, because only updated data is exchanged
  • Data updates are instantly visible on multiple clients without refreshing

The AspNetCore.Realtime is database independent, because it works from an API point of view. Simple calls added to your API endpoint, will publish updates, inserts, deletes to all listening clients.

Example:

[HttpPost]
[Authorize]
[Consumes("application/json")]
public async Task<IActionResult> AddAsync([FromBody] PersonDto personDto, CancellationToken cancellationToken)
{
    var person = _mapper.Map<Person>(personDto);

    // Find the account claim
    var accountClaim = User.Claims.SingleOrDefault(c => c.Type == DemoClaimTypes.AccountId);
    if (accountClaim == null) return Unauthorized();

    // Get accountId from accountClaim
    var accountId = accountClaim.Value;

    // Use repository to create a new person record
    var addedPerson = await _personRepository.AddAsync(
        accountId,
        person,
        cancellationToken);
    var addedPersonDto = _mapper.Map<PersonDto>(addedPerson);

    // After successful creation, trigger AspNetCore.Realtime to
    // synchronize all clients where authorization fits. In this
    // example the authorization is based on users with that
    // belong to the same account
    _ = _collectionTrigger.OnInsertedAsync(
        CollectionName,
        addedPersonDto,
        session => session.Claims.ValidateAccount(accountId),
        cancellationToken);

    return Ok(addedPersonDto);
}

Every client that is subscribed to a dataset will get updates, within your own defined authorization scope ofcourse, even if you're not the initiator of the action.

For example user1 adds a record to the database, than user1 gets to see the new record, but also user2 and user4.

Installation

Install Nuget package AspNetCore.Realtime

Add-Package AspNetCore.Realtime

Install npm package react-realtime

npm install --save react-realtime

Usage

AspNetCore startup add:

    builder.Services
        .AddRealtime()
        .AddCollections();

    ...

    app.UseRealtime();

Or with authentication:

    builder.Services
        .AddRealtime()
        .AddJwtAuthentication(authBuilder =>
        {
            authBuilder.Audience = builder.Configuration["Jwt:Audience"] ?? string.Empty;
            authBuilder.Issuer = builder.Configuration["Jwt:Issuer"] ?? string.Empty;
            authBuilder.Secret = builder.Configuration["Jwt:Key"] ?? string.Empty;
        })
        .AddCollections();

    ...

    app.UseRealtime();

AddRealtime

This registeres the publish and subscribe system

AddCollections

This method registers all collections that you want to publish. It scans all assemblies for classes which are derived from EntityCollection and are decorated with the Publication attribute. The publication attribute defines the name of the publication.

The EntityCollection contains 2 types. One public model (Dto) and one private model. It uses Automapper for any model mapping.

The public models are used for transfer between client and server. The private model is used internally by you. For example for storing in a database.

[Publication("persons")]
public class PersonCollection : EntityCollection<PersonDto, Person>

About

It is a publish and subscribe system between client (Javascript) and server (AspNetCore). It vastly simplifies the client programming.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors