Silky Microservice FrameworkSilky Microservice Framework
Home
Docs
Config
Source
github
gitee
  • 简体中文
  • English
Home
Docs
Config
Source
github
gitee
  • 简体中文
  • English
  • Introduction

    • Silky Framework Overview
  • Getting Started

    • Glossary
    • Quick Start
    • Project Template
    • Microservice Architecture
    • Sample Projects
  • Host & Module

    • Host Types
    • Gateway Configuration
    • Module System
    • Plugin System
  • Gateway & HTTP

    • Swagger / OpenAPI
    • MiniProfiler
    • CORS
    • Audit Logging
  • Service & RPC

    • App Services & Service Entries
    • RPC Communication
    • WebSocket
    • Service Registry
    • Service Governance
  • Data & Cache

    • EFCore Data Access
    • Caching
    • Distributed Lock
  • Security & Auth

    • Identity & Authentication
    • Distributed Transactions
  • Infrastructure

    • Dependency Injection
    • Object Mapping
    • Validation
    • Link Tracking (SkyAPM)
    • Logging (Serilog)
    • Health Checks
    • Message Bus (MassTransit)
    • Unit & Integration Testing

Overview

Silky provides WebSocket support via the Silky.WebSocket package, enabling server-initiated push messages to connected clients. This is ideal for real-time scenarios such as notifications, chat, live dashboards, and streaming.

Installation

<PackageReference Include="Silky.WebSocket" Version="3.9.2" />

Host Setup

Use ConfigureSilkyWebSocketHostDefaults instead of the standard host builder:

// Program.cs
await Host.CreateDefaultBuilder(args)
    .ConfigureSilkyWebSocketHostDefaults<NotificationHostModule>(webBuilder =>
    {
        webBuilder.UseStartup<Startup>();
    })
    .Build()
    .RunAsync();

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSilkyHttpCore()
            .AddWebSocketCore();
}

public void Configure(IApplicationBuilder app)
{
    app.UseWebSockets();
    app.UseSilkyWebSocket();
    app.UseSilkyRpcProxy();
}

Module

[DependsOn(
    typeof(WebSocketModule),
    typeof(ZookeeperModule),
    typeof(DotNettyTcpModule)
)]
public class NotificationHostModule : SilkyModule { }

WebSocket Service Interface

Define a WebSocket service the same way as a regular application service, but annotate with [WsRoute]:

[WsRoute]
public interface INotificationService
{
    Task SendNotificationAsync(NotificationInput input);
    Task BroadcastAsync(BroadcastInput input);
}

Implementation

public class NotificationService : INotificationService, IScopedDependency
{
    private readonly IWsSessionManager _sessionManager;

    public NotificationService(IWsSessionManager sessionManager)
    {
        _sessionManager = sessionManager;
    }

    public async Task SendNotificationAsync(NotificationInput input)
    {
        // Send to a specific user session
        var session = _sessionManager.GetByUserId(input.UserId);
        if (session != null)
        {
            await session.SendAsync(input.Message);
        }
    }

    public async Task BroadcastAsync(BroadcastInput input)
    {
        // Broadcast to all connected clients
        foreach (var session in _sessionManager.GetAll())
        {
            await session.SendAsync(input.Message);
        }
    }
}

Client Connection

Clients connect to the WebSocket endpoint:

const ws = new WebSocket('ws://gateway-host/ws/notification');

ws.onmessage = (event) => {
    const notification = JSON.parse(event.data);
    console.log('Received:', notification);
};

ws.onopen = () => {
    console.log('WebSocket connected');
};

Configuration

{
  "webSocket": {
    "keepAliveInterval": 120,
    "receiveBufferSize": 4096,
    "path": "/ws"
  }
}
OptionDefaultDescription
keepAliveInterval120Keep-alive ping interval in seconds
receiveBufferSize4096Receive buffer size in bytes
path"/ws"WebSocket connection path prefix
Edit this page
Prev
RPC Communication
Next
Service Registry