Implement real-time communication with SignalR
✓Works with OpenClaudeYou are a .NET backend developer implementing real-time bidirectional communication using SignalR hubs and connections.
What to check first
- Verify
Microsoft.AspNetCore.SignalRNuGet package is installed:dotnet list package - Check your ASP.NET Core project targets
.NET 6.0or higher in.csproj - Confirm you have a running ASP.NET Core host (Kestrel, IIS, or similar)
Steps
- Create a Hub class inheriting from
Hub<T>where T is your client interface contract with method signatures clients will call - Implement server-side methods in the Hub that clients can invoke using
Clients.All.SendAsync(),Clients.Caller.SendAsync(), orClients.Others.SendAsync() - Register SignalR in
Program.csusingbuilder.Services.AddSignalR()before building the app - Map the Hub endpoint in middleware with
app.MapHub<YourHub>("/hub-path")beforeapp.Run() - Define a public interface on the Hub that declares all client-callable methods with
Taskreturn types - Use
Clients.Group()andGroups.AddToGroupAsync()to organize clients into named groups for targeted messaging - Implement
OnConnectedAsync()andOnDisconnectedAsync()overrides to track connection lifecycle events - Call server methods from clients using the HubConnection object with
.InvokeAsync<T>("MethodName", args)or.SendAsync("MethodName", args)
Code
using Microsoft.AspNetCore.SignalR;
using System.Collections.Concurrent;
// Define the client contract interface
public interface IChatClient
{
Task ReceiveMessage(string user, string message);
Task UserConnected(string user);
Task UserDisconnected(string user);
Task ReceiveNotification(string notification);
}
// Hub implementation
public class ChatHub : Hub<IChatClient>
{
private static readonly ConcurrentDictionary<string, string> Users = new();
public override async Task OnConnectedAsync()
{
string user = Context.User?.Identity?.Name ?? $"Guest-{Context.ConnectionId}";
Users.TryAdd(Context.ConnectionId, user);
await Clients.All.UserConnected(user);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
if (Users.TryRemove(Context.ConnectionId, out var user))
{
await Clients.All.UserDisconnected(user);
}
await base.OnDisconnectedAsync(exception);
}
public async Task SendMessage(string message)
{
if (!Users.TryGetValue(Context.ConnectionId, out var user))
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related .NET / C# Skills
Other Claude Code skills in the same category — free to download.
ASP.NET Core API
Scaffold ASP.NET Core Web API with controllers and middleware
Entity Framework Core
Set up Entity Framework Core with migrations and queries
ASP.NET Auth
Configure ASP.NET Identity with JWT and cookie authentication
Minimal API
Build minimal APIs with endpoint mapping
Blazor
Create Blazor components for interactive web UI
.NET Testing
Write xUnit tests with Moq and FluentAssertions
.NET Background Services
Create background services with IHostedService
Want a .NET / C# skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.