Samhammer.SocketServer.ApiClient 8.0.2

dotnet add package Samhammer.SocketServer.ApiClient --version 8.0.2
NuGet\Install-Package Samhammer.SocketServer.ApiClient -Version 8.0.2
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="Samhammer.SocketServer.ApiClient" Version="8.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Samhammer.SocketServer.ApiClient --version 8.0.2
#r "nuget: Samhammer.SocketServer.ApiClient, 8.0.2"
#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 Samhammer.SocketServer.ApiClient as a Cake Addin
#addin nuget:?package=Samhammer.SocketServer.ApiClient&version=8.0.2

// Install Samhammer.SocketServer.ApiClient as a Cake Tool
#tool nuget:?package=Samhammer.SocketServer.ApiClient&version=8.0.2

Samhammer.SocketServer

Note: The implementation is currently uni directional and only ment for communication from the server to the client.

Note2: This repo is private for multiple reasons, even though the server project would be reusable:

  • To not disclose infrastructure information over the appsettings of the api
  • Because the api docker image is in our private docker registry

Samhammer.SocketServer.Api

This is an entire api project that uses Samhammer.SocketServer.Server to provide endpoints for it's functionality. The project is provided as docker image.

How to use

Take a look at appsettings because there are a few settings that need to be overwritten via env vars. (at least the Elastic index and both AuthOptions blocks)

Docker images are pushed to registry.samhammer.oncloudandheat.com. See project "samhammer-socket-server" there.

The following API endpoints are provided:

  • /ws ⇒ Websocket endpoint
  • GET api/client ⇒ Get all active client ids
  • POST api/notification/<clientid> ⇒ Send a message to a specific client
  • POST api/notification/broadcast ⇒ Send a message to all clients

Samhammer.SocketServer.ApiClient

Contains the HTTP-Calls to access the "Samhammer.SocketServer.Api" project.

Install the nuget package "Samhammer.SocketServer.ApiClient".

Then you can Inject "ISocketServerApiClient". (it is registered in ioc by Samhammer.DependencyInjection)

Also register a matching http client like that:

    builder.Services
        .AddHttpClient<ISocketServerApiClient, SocketServerApiClient>(options =>
            options.BaseAddress = new Uri(builder.Configuration["SocketServerApiOptions:Url"]))
        .AddClientCredentialsTokenHandler(ApiAuthOptions.DefaultClientName); // https://github.com/DuendeSoftware/Duende.AccessTokenManagement

Samhammer.SocketServer.ApiClient.JS

Contains the HTTP-Calls to access the "Samhammer.SocketServer.Api" from javascript. This client is provided as npm package.

Install the npm package "@samhammer/socketserver-apiclient-js".

Implement the abstract class like that:

class MyProjectWebsocketNotificationService extends WebsocketNotificationService {
    public init() {
        const myUrl = "...";
        const myDeviceId = "...";
        super.init(socketUrl, deviceId.value);
    }

    protected async onSendPushToken(token: PushTokenContract): Promise<void> {
        // There you can do logic as soon as the clients got a push token
        // e.g. calling your api to save it
    }

    protected async onDeletePushToken(deviceId: string): Promise<void> {
        // There you can do logic if the push token needs to be deleted
        // e.g. calling your api to delete it
    }

    protected onPushMessage(socketJson: unknown): Promise<void> {
        // This function is called as soon as a push message
        // is received. The content depends on what you send from the server
    }

    protected onConnectionStatusChanged(isConnected: boolean): Promise<void> {
        // This method is called if the connection status changes.
        // If you need to know the status in your application, you can put your logic here.
        // e.g. saving the state to a pinia store
    }
}

Samhammer.SocketServer.Server

A library to integrate websocket technology into an existing api project.

What can it do:

  • Provide endpoint of a socket server
  • Establish and keep open connections
  • Send messages from the server to one or multiple clients
  • Handles clientId stuff (first message sent to the client is the clientId)

How to use

Install the nuget package "Samhammer.SocketServer.Server".

Adjust jour Program.cs like that:

    builder.Services.AddSocketServer(builder.Configuration); // There are overloads for different scenarios 

    var app = builder.Build();

    app.MapSocketServer("/ws"); // Endpoint under which the socket server runs

If you use "AddSocketServer(builder.Configuration)" add this config block to appsettings:

  "SocketServerOptions": {
    "HeartbeatIntervalSeconds": 300,
    "MaxBufferSizeInKb": 500
  },
  • HeartbeatIntervalSeconds: Configure how often keep alive messages should be transfered
  • MaxBufferSizeInKb: Max message lengt. Everything langer will be cut off

Inject IWebsocketService to use the server:

  • Task<bool> SendText(string clientId, string text) ⇒ Send a message to a specific client (returns true in case of success)
  • Task BroadcastText(string text) ⇒ Send a message to all connected clients
  • List<string> GetActiveClients() ⇒ Get a list of active client ids
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
8.0.2 66 6/10/2024
8.0.1 175 5/31/2024
8.0.0 56 5/31/2024