{"library":"signalr","title":"ASP.NET SignalR 2.x JavaScript Client","description":"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.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install signalr"],"cli":null},"imports":["import jQuery from 'jquery'; // Or ensure global jQuery is loaded via script tag","import 'jquery'; import 'signalr'; // Then `$.hubConnection` is available globally or via `jQuery.hubConnection`","import 'jquery'; import 'signalr'; // Then ensure /signalr/hubs is loaded; `$.connection` becomes available"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"<!-- index.html (full example demonstrating loading order) -->\n<!DOCTYPE html>\n<html>\n<head>\n    <title>SignalR Client Example</title>\n</head>\n<body>\n    <div id=\"discussion\"></div>\n    <input type=\"text\" id=\"message\" placeholder=\"Enter message\"/>\n    <input type=\"button\" id=\"sendmessage\" value=\"Send\" />\n\n    <!-- 1. Ensure jQuery is loaded first -->\n    <script src=\"https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.7.1.min.js\"></script>\n    <!-- 2. Then the SignalR client -->\n    <script src=\"https://ajax.aspnetcdn.com/ajax/signalr/jquery.signalr-2.4.3.min.js\"></script>\n    <!-- 3. Then the auto-generated hub proxies from your server -->\n    <!-- This URL MUST point to your ASP.NET SignalR server's hubs endpoint -->\n    <script src=\"http://localhost:5000/signalr/hubs\"></script>\n\n    <script type=\"text/javascript\">\n        $(function () {\n            console.log(\"jQuery and SignalR client scripts loaded.\");\n\n            // $.connection contains proxies for server-side hubs (e.g., $.connection.chatHub).\n            // The /signalr/hubs script makes this available.\n            if ($.connection && $.connection.hub) {\n                const hubConnection = $.connection.hub; // The main SignalR connection object\n\n                // Define a client-side method that the server can call\n                hubConnection.client.addMessage = function (name, message) {\n                    console.log(`Server called addMessage: ${name}: ${message}`);\n                    $('<li>').text(`${name}: ${message}`).appendTo('#discussion');\n                };\n\n                // Start the connection to the SignalR server\n                hubConnection.start()\n                    .done(function () {\n                        console.log(\"Connected to SignalR hub. Connection ID: \" + hubConnection.id);\n                        $('#sendmessage').click(function () {\n                            const message = $('#message').val();\n                            if (message) {\n                                // Invoke a server-side method. 'Send' is a common method name.\n                                // 'ChatHub' is the name of your server-side hub class.\n                                // Arguments follow the method name.\n                                hubConnection.invoke('Send', 'ClientUser', message)\n                                    .fail(function(error) {\n                                        console.error('Error invoking server method:', error);\n                                    });\n                                $('#message').val('');\n                            }\n                        });\n                    })\n                    .fail(function (error) {\n                        console.error(\"Could not connect to SignalR hub: \" + error);\n                    });\n            } else {\n                console.warn(\"SignalR client or hub proxies not fully loaded. Check script loading order and server /signalr/hubs endpoint.\");\n            }\n        });\n    </script>\n</body>\n</html>","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}