ASP.NET SignalR 2.x JavaScript Client

2.4.3 · maintenance · verified Sun Apr 19

The `signalr` npm package provides the official JavaScript client for Microsoft's **ASP.NET SignalR 2.x** framework, enabling real-time web functionality such as bi-directional communication between server and client. This client is specifically designed to interact with ASP.NET (full .NET Framework) server applications and is distinct from the newer `ASP.NET Core SignalR` client found under `@microsoft/signalr`. The current stable version is 2.4.3, which primarily includes bug fixes and minor improvements, reflecting its status as a maintenance-mode library. Releases are generally patch-focused with no new features planned. A key characteristic is its reliance on jQuery, functioning as a jQuery plugin to provide its `$.hubConnection` API, and its compatibility with the older ASP.NET runtime environment.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to load the SignalR client, establish a connection to an ASP.NET SignalR server, define a client-side method, and invoke a server-side method for basic real-time communication within an HTML page.

<!-- index.html (full example demonstrating loading order) -->
<!DOCTYPE html>
<html>
<head>
    <title>SignalR Client Example</title>
</head>
<body>
    <div id="discussion"></div>
    <input type="text" id="message" placeholder="Enter message"/>
    <input type="button" id="sendmessage" value="Send" />

    <!-- 1. Ensure jQuery is loaded first -->
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.7.1.min.js"></script>
    <!-- 2. Then the SignalR client -->
    <script src="https://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.4.3.min.js"></script>
    <!-- 3. Then the auto-generated hub proxies from your server -->
    <!-- This URL MUST point to your ASP.NET SignalR server's hubs endpoint -->
    <script src="http://localhost:5000/signalr/hubs"></script>

    <script type="text/javascript">
        $(function () {
            console.log("jQuery and SignalR client scripts loaded.");

            // $.connection contains proxies for server-side hubs (e.g., $.connection.chatHub).
            // The /signalr/hubs script makes this available.
            if ($.connection && $.connection.hub) {
                const hubConnection = $.connection.hub; // The main SignalR connection object

                // Define a client-side method that the server can call
                hubConnection.client.addMessage = function (name, message) {
                    console.log(`Server called addMessage: ${name}: ${message}`);
                    $('<li>').text(`${name}: ${message}`).appendTo('#discussion');
                };

                // Start the connection to the SignalR server
                hubConnection.start()
                    .done(function () {
                        console.log("Connected to SignalR hub. Connection ID: " + hubConnection.id);
                        $('#sendmessage').click(function () {
                            const message = $('#message').val();
                            if (message) {
                                // Invoke a server-side method. 'Send' is a common method name.
                                // 'ChatHub' is the name of your server-side hub class.
                                // Arguments follow the method name.
                                hubConnection.invoke('Send', 'ClientUser', message)
                                    .fail(function(error) {
                                        console.error('Error invoking server method:', error);
                                    });
                                $('#message').val('');
                            }
                        });
                    })
                    .fail(function (error) {
                        console.error("Could not connect to SignalR hub: " + error);
                    });
            } else {
                console.warn("SignalR client or hub proxies not fully loaded. Check script loading order and server /signalr/hubs endpoint.");
            }
        });
    </script>
</body>
</html>

view raw JSON →