SignalR hub configuration in ASP.NET Core application

18 Nov 20182 minutes to read

Overview

This guide explains how to configure SignalR Hub in an ASP.NET Core application for real-time collaborative diagram editing.

Prerequisites

How to create ASP.NET Core application

To create an ASP.NET Core application, follow the steps outlined in the ASP.NET Core Getting Started documentation.

How to add packages in the ASP.NET Core application

Open the NuGet Package Manager and install the following packages.

  • Microsoft.AspNetCore.SignalR.Client

Configure SignalR service in ASP.NET Core application

To enable real-time collaboration, configure SignalR HubConnection in your ASP.NET Core view/controller as follows:

  • Initialize the HubConnection and start it using start().
  • Connect to the /diagramHub endpoint with WebSocket transport skipNegotiation: true and enable automatic reconnect to handle transient network issues.
  • Subscribe to the OnConnectedAsync callback to receive the unique connection ID, confirming a successful handshake with the server.
  • Join a SignalR group by calling JoinDiagram(roomName) after connecting. This ensures updates are shared only with users in the same diagram session.
  • Refer to Create ASP.NET Core Simple Diagram

Notes:

  • Use a unique roomName per diagram (e.g., a diagram ID) to isolate sessions.
  • If WebSockets may be unavailable, remove SkipNegotiation so SignalR can fall back to SSE or Long Polling.
  • Consider handling reconnecting/disconnected states in the UI and securing the connection with authentication, if required.
  • For ASP.NET Core, place this script in your shared layout or specific view where the diagram is hosted.

Sending and applying real-time diagram changes

  • The ASP.NET Core Diagram component triggers the historyChange event whenever the diagram is modified (e.g., add, delete, move, resize, or edit nodes/connectors).
  • Use getDiagramUpdates to produce a compact set of incremental updates (JSON-formatted changes) representing just the changes, not the entire diagram.
  • Send these changes to the hub method BroadcastToOtherUsers, which relays them to all users joined to the same SignalR group (room).
  • Each remote user listens for ReceiveData and applies the incoming changes with setDiagramUpdates, keeping their view synchronized without reloading the full diagram.
  • Enable the enableCollaborativeEditing property on the diagram to treat multi-step edits (like drag/resize sequences or batch changes) as a single operation.

Conflict policy (optimistic concurrency) in ASP.NET Core application

To maintain consistency during collaborative editing, each user applies incoming changes using setDiagramUpdates. After applying changes, the ASP.NET Core sample synchronizes its userVersion with the serverVersion through the UpdateVersion event. This version-based approach ensures conflicts are resolved without locking, allowing real-time responsiveness while preserving data integrity.

Add the following code in the ASP.NET Core application: