ResgateIO.Service 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package ResgateIO.Service --version 0.2.0
NuGet\Install-Package ResgateIO.Service -Version 0.2.0
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="ResgateIO.Service" Version="0.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ResgateIO.Service --version 0.2.0
#r "nuget: ResgateIO.Service, 0.2.0"
#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 ResgateIO.Service as a Cake Addin
#addin nuget:?package=ResgateIO.Service&version=0.2.0

// Install ResgateIO.Service as a Cake Tool
#tool nuget:?package=ResgateIO.Service&version=0.2.0

RES Service for .NET : Synchronize Your Clients

Library used to create REST, real time, and RPC APIs, where all your reactive web clients are synchronized seamlessly through Resgate.

Visit Resgate.io for more information on the project.
Visit the GitHub repository for complete examples with both client and server.

As easy as

ResService service = new ResService("example");
service.AddHandler("model", new DynamicHandler()
    .SetGet(r => r.Model(new { message = "Hello, World!" }))
    .SetAccess(r => r.AccessGranted()));
service.Serve("nats://127.0.0.1:4222");

Basic usage

Create a new service
ResService service = new ResService("myservice");
Define a handler class for a model resource
class MyModelHandler : ModelHandler
{
    private readonly object model = new {
        message = "Hello, .NET World!"
    }

    public override void Get(IModelRequest request)
    {
        request.Model(model);
    }
}
Define a handler class for a collection resource
class MyModelHandler : CollectionHandler
{
    private readonly object[] collection = new object[]{
        "first", "second", "third"
    }

    public override void Get(ICollectionRequest request)
    {
        request.Collection(collection);
    }
}
Add handler for a resource
service.AddHandler("model", new MyResourceHandler());
Add handlers for a collection resource
mycollection := []string{"first", "second", "third"}
s.Handle("mycollection",
   res.Access(res.AccessGranted),
   res.GetCollection(func(r res.CollectionRequest) {
      r.Collection(mycollection)
   }),
)
Add handlers for parameterized resources
service.AddHandler("article.$id", new DynamicHandler()
    .SetAccess(r => r.AccessGranted())
    .SetModelGet(r =>
    {
        if (DB.TryGetArticle(r.PathParams["id"], out Article article))
            r.Model(article);
        else
            r.NotFound();
    }));
Add handlers for method calls
service.AddHandler("math", new DynamicHandler()
    .SetCallMethod("double", r =>
    {
        r.Ok(2 * (double)r.Params["value"]);
    }));
Send change event on model update

A change event will update the model on all subscribing clients.

MyModel mymodel = new MyModel { Name = "foo" };
service.With("example.mymodel", resource =>
{
    mymodel.Name = "bar";
    resource.ChangeEvent(new Dictionary<string, object> { { "name", "bar" } });
});
Send add event on collection update:

An add event will update the collection for all subscribing clients.

var mycollection = new List<string> { "first", "second" };
service.With("example.mycollection", resource =>
{
    resource.AddEvent("third", mycollection.Count);
    mycollection.Add("third");
});
Add handlers for authentication
service.AddHandler("myauth", new DynamicHandler()
    .SetAuthMethod("login", r =>
    {
        if ((string)r.Params["password"] == "mysecret")
        {
            r.TokenEvent(new { user = "admin" });
            r.Ok();
        }
        else
        {
            r.InvalidParams("Wrong password");
        }
    }));
Add handlers for access control (with wildcard ">")
service.AddHandler(">", new DynamicHandler()
    .SetAccess(r =>
    {
        if (r.Token != null && (string)r.Token["user"] == "admin")
            r.AccessGranted();
        else
            r.AccessDenied();
    }));
Start service
service.Serve("nats://127.0.0.1:4222");

Contributing

The ResgateIO.Service library is still under development, and minor versions may still contain breaking changes in the API. Any feedback on the package API or its implementation is highly appreciated!

If you find any issues, feel free to report them as an issue on GitHub.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
0.5.0 3,728 5/24/2023
0.4.8 922 10/25/2021
0.4.7 350 10/18/2021
0.4.6 325 9/24/2021
0.4.5 1,210 4/27/2021
0.4.4 1,122 3/10/2021
0.4.3 670 2/13/2020
0.4.2 568 11/28/2019
0.4.1 459 10/21/2019
0.3.1 534 8/29/2019
0.3.0 494 8/27/2019
0.2.0 489 8/22/2019
0.1.0 465 8/15/2019

Stability and functionality improvements.