Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

SignalR Real-Time Communication Implementation

Tech May 16 1

Real-Time Server-Client Communication with SignalR

SignalR enables servers to push real-time updates to connected clients. Multiple clients connect to a server, where one client's action triggers server-side events that broadcast to all connected cliants.

.NET Framework Implementation

Server Configuration

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

Hub Implementation

public class CommunicationHub : Hub
{
    public void BroadcastMessage(string messageContent)
    {
        Clients.All.ReceiveMessage(messageContent);
    }
}

Client-Side Implementation (JavaScript)

<script src="jquery.js"></script>
<script src="jquery.signalR.js"></script>
<script src="/signalr/hubs"></script>

<script>
const messageHub = $.connection.communicationHub;

messageHub.client.receiveMessage = function(content) {
    alert(content);
};

$.connection.hub.start().done(function() {
    messageHub.server.broadcastMessage("Sample content");
});
</script>

Broadcast Methods

  • Clients.All: Broadcast to all clients
  • Clients.Caller: Target only the initiating client
  • Clients.Others: Broadcast to all except initiatro
  • Clients.Client(connectionId): Target specific client
  • Clients.Group(groupId): Broadcast to group members

.NET Core Implementation

Startup Configuration

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
}

public void Configure(IApplicationBuilder app)
{
    app.UseSignalR(routes => 
    {
        routes.MapHub<CommunicationHub>("/messageHub");
    });
}

Core Hub Implementation

public class CommunicationHub : Hub
{
    public async Task BroadcastMessage(string messageContent)
    {
        await Clients.All.InvokeAsync("ReceiveMessage", messageContent);
    }
}

Client-Side (Core)

<script src="signalr-client.js"></script>

<script>
const connection = new signalR.HubConnection("/messageHub");
connection.start();

connection.on("ReceiveMessage", content => {
    document.querySelector("ul").innerHTML += `<li>${content}</li>`;
});

document.getElementById("send").addEventListener("click", () => {
    connection.invoke("BroadcastMessage", "New content");
});
</script>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.