
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
@microsoft/signalr
Advanced tools
@microsoft/signalr is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. It allows server-side code to push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.
Establishing a Connection
This code demonstrates how to establish a connection to a SignalR hub. The `HubConnectionBuilder` is used to configure and create a connection to the specified hub URL.
const signalR = require('@microsoft/signalr');
const connection = new signalR.HubConnectionBuilder()
.withUrl('/chatHub')
.build();
connection.start().then(() => console.log('Connection started')).catch(err => console.error(err));
Sending Messages
This code shows how to send a message to the server using the `invoke` method. The first parameter is the name of the server method to call, and the subsequent parameters are the arguments to pass to that method.
connection.invoke('SendMessage', 'Hello, World!').catch(err => console.error(err));
Receiving Messages
This code demonstrates how to set up a client-side handler for messages sent from the server. The `on` method is used to specify the name of the server method to handle and a callback function to execute when the method is called.
connection.on('ReceiveMessage', (user, message) => {
console.log(`${user}: ${message}`);
});
Socket.IO is a popular library for real-time web applications. It enables real-time, bidirectional and event-based communication. It works on every platform, browser, or device, focusing equally on reliability and speed. Compared to @microsoft/signalr, Socket.IO is more widely used and has a larger community, but it may require more setup and configuration.
Primus is a flexible and extensible library for real-time communication. It abstracts away the different real-time frameworks and provides a unified API. Primus can work with various real-time engines like WebSockets, Engine.IO, and SockJS. It offers more flexibility compared to @microsoft/signalr but might be more complex to set up.
Faye is a simple publish-subscribe messaging system based on the Bayeux protocol. It is designed for low-latency, high-throughput messaging between web clients. Faye is simpler and more lightweight compared to @microsoft/signalr, but it may not offer as many features out of the box.
JavaScript and TypeScript clients for SignalR for ASP.NET Core
npm install @microsoft/signalr
or
yarn add @microsoft/signalr
See the SignalR Documentation at docs.microsoft.com for documentation on the latest release. API Reference Documentation is also available on docs.microsoft.com.
To use the client in a browser, copy *.js
files from the dist/browser
folder to your script folder include on your page using the <script>
tag.
To use the client in a webworker, copy *.js
files from the dist/webworker
folder to your script folder include on your webworker using the importScripts
function. Note that webworker SignalR hub connection supports only absolute path to a SignalR hub.
To use the client in a NodeJS application, install the package to your node_modules
folder and use require('@microsoft/signalr')
to load the module. The object returned by require('@microsoft/signalr')
has the same members as the global signalR
object (when used in a browser).
let connection = new signalR.HubConnectionBuilder()
.withUrl("/chat")
.build();
connection.on("send", data => {
console.log(data);
});
connection.start()
.then(() => connection.invoke("send", "Hello"));
importScripts('signalr.js');
let connection = new signalR.HubConnectionBuilder()
.withUrl("https://example.com/signalr/chat")
.build();
connection.on("send", data => {
console.log(data);
});
connection.start()
.then(() => connection.invoke("send", "Hello"));
const signalR = require("@microsoft/signalr");
let connection = new signalR.HubConnectionBuilder()
.withUrl("/chat")
.build();
connection.on("send", data => {
console.log(data);
});
connection.start()
.then(() => connection.invoke("send", "Hello"));
FAQs
ASP.NET Core SignalR Client
The npm package @microsoft/signalr receives a total of 417,705 weekly downloads. As such, @microsoft/signalr popularity was classified as popular.
We found that @microsoft/signalr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.