1 | JavaScript and TypeScript clients for SignalR for ASP.NET Core
|
2 |
|
3 | ## Installation
|
4 |
|
5 | ```bash
|
6 | npm install @microsoft/signalr
|
7 | ```
|
8 | or
|
9 | ```bash
|
10 | yarn add @microsoft/signalr
|
11 | ```
|
12 |
|
13 | ## Usage
|
14 |
|
15 | See the [SignalR Documentation](https://docs.microsoft.com/en-us/aspnet/core/signalr) at docs.microsoft.com for documentation on the latest release. [API Reference Documentation](https://docs.microsoft.com/javascript/api/%40aspnet/signalr/?view=signalr-js-latest) is also available on docs.microsoft.com.
|
16 |
|
17 | ### Browser
|
18 |
|
19 | 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.
|
20 |
|
21 | ### WebWorker
|
22 |
|
23 | 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.
|
24 |
|
25 | ### Node.js
|
26 |
|
27 | 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).
|
28 |
|
29 | ### Example (Browser)
|
30 |
|
31 | ```JavaScript
|
32 | let connection = new signalR.HubConnectionBuilder()
|
33 | .withUrl("/chat")
|
34 | .build();
|
35 |
|
36 | connection.on("send", data => {
|
37 | console.log(data);
|
38 | });
|
39 |
|
40 | connection.start()
|
41 | .then(() => connection.invoke("send", "Hello"));
|
42 | ```
|
43 |
|
44 | ### Example (WebWorker)
|
45 |
|
46 |
|
47 | ```JavaScript
|
48 | importScripts('signalr.js');
|
49 |
|
50 | let connection = new signalR.HubConnectionBuilder()
|
51 | .withUrl("https://example.com/signalr/chat")
|
52 | .build();
|
53 |
|
54 | connection.on("send", data => {
|
55 | console.log(data);
|
56 | });
|
57 |
|
58 | connection.start()
|
59 | .then(() => connection.invoke("send", "Hello"));
|
60 |
|
61 | ```
|
62 |
|
63 | ### Example (NodeJS)
|
64 |
|
65 | ```JavaScript
|
66 | const signalR = require("@microsoft/signalr");
|
67 |
|
68 | let connection = new signalR.HubConnectionBuilder()
|
69 | .withUrl("/chat")
|
70 | .build();
|
71 |
|
72 | connection.on("send", data => {
|
73 | console.log(data);
|
74 | });
|
75 |
|
76 | connection.start()
|
77 | .then(() => connection.invoke("send", "Hello"));
|
78 | ```
|