signalr
  1. signalr-hub-pipeline

SignalR Hub Pipeline

The SignalR Hub Pipeline is a collection of middleware components that provide a way to intercept, modify, and respond to requests and responses between clients and a SignalR hub. To take advantage of the pipeline, you need to have a good understanding of how it works.

Syntax

app.UseSignalR(options =>
{
   options.EnableDetailedErrors = true;
   options.KeepAliveInterval = TimeSpan.FromSeconds(30);
   options.ClientTimeoutInterval = TimeSpan.FromSeconds(30);
 
   // Add middleware
   options.AddHubPipeline(x => {
      x.Use<MyMiddleware>();
   });
});

Example

Here's an example of how to use the SignalR Hub Pipeline to create a middleware component that logs all incoming requests to a hub:

public class LoggingMiddleware
{
   private readonly ILogger<LoggingMiddleware> _logger;
 
   public LoggingMiddleware(ILogger<LoggingMiddleware> logger)
   {
      _logger = logger;
   }
 
   public async Task InvokeAsync(HubInvocationContext context, Func<HubInvocationContext, Task> next)
   {
      _logger.LogInformation($"Received {context.HubMethodName} request from {context.ConnectionContext.ConnectionId}");
 
      await next(context);
   }
}

To use this middleware component, you need to add it to the SignalR Hub Pipeline:

app.UseSignalR(options =>
{
   options.AddHubPipeline(x => {
      x.Use<LoggingMiddleware>();
   });
 
   // ...
});

Output

When a request is made to the hub, the middleware component logs a message to the console or to a logging framework of your choice:

info: MyNamespace.LoggingMiddleware[0]
      Received MyHubMethod request from 12345678-1234-1234-1234-123456789012

Explanation

The SignalR Hub Pipeline is a way to add middleware components to a hub that can modify, intercept, or respond to requests and responses from clients. Middleware components are functions that take a HubInvocationContext and a Func<HubInvocationContext, Task> as parameters. The context contains information about the request, such as the hub method name, connection context, and arguments. The next parameter is a function that passes the request to the next middleware component in the pipeline.

Middleware components can be added to the pipeline using the AddHubPipeline method of the SignalRHubOptions object, which is passed to the UseSignalR method. Middleware components are added to the pipeline using the Use method of the IHubPipelineBuilder object, which is passed to the AddHubPipeline method.

Use

The SignalR Hub Pipeline can be used for a variety of tasks, such as logging, authentication, rate limiting, and caching. Middleware components can be added in any order, and can modify the context or the response before passing it to the next middleware component.

Important Points

  • The SignalR Hub Pipeline is a collection of middleware components that intercept, modify, or respond to requests and responses from clients.
  • Middleware components are added to the pipeline using the AddHubPipeline method of the SignalRHubOptions object.
  • Middleware components are added using the Use method of the IHubPipelineBuilder object.

Summary

The SignalR Hub Pipeline is a powerful tool that can be used for a variety of tasks, such as logging, authentication, and rate limiting. Middleware components can intercept, modify, or respond to requests and responses from clients, and can be added in any order. To use the pipeline, you need to understand how middleware components work, and how they can be added to the pipeline.

Published on: