SignalR hub configuration in ASP.NET MVC application
18 Nov 20182 minutes to read
Overview
This guide explains how to configure SignalR Hub in an ASP.NET MVC application for real-time collaborative diagram editing.
Prerequisites
How to create ASP.NET MVC application
To create an ASP.NET MVC application, follow the steps outlined in the ASP.NET MVC Getting Started documentation.
How to add packages in the ASP.NET MVC application
Open the NuGet Package Manager and install the following packages.
- Microsoft.AspNetCore.SignalR.Client
Configure SignalR service in ASP.NET MVC application
To enable real-time collaboration, configure SignalR HubConnection in your ASP.NET MVC view/controller as follows:
- Initialize the
HubConnectionand start it usingstart(). - Connect to the
/diagramHubendpoint with WebSocket transportskipNegotiation: trueand enable automatic reconnect to handle transient network issues. - Subscribe to the
OnConnectedAsynccallback 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 MVC Simple Diagram
Notes:
- Use a unique
roomNameper diagram (e.g., a diagram ID) to isolate sessions.- If
WebSocketsmay be unavailable, removeSkipNegotiationso 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 MVC, 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 MVC 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
enableCollaborativeEditingproperty 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 MVC application
To maintain consistency during collaborative editing, each user applies incoming changes using setDiagramUpdates. After applying changes, the ASP.NET MVC 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 MVC application: