Skip to main content

Connecting

Here we describe in general what is SignalR and how to connect to the hub on the server.

The SignalR libraries are split into two parts: server and client.
The server libraries are built-into ASP.NET Core packages and project templates, and the client library is a simple JavaScript file that can be downloaded and added to any project.
For using the API, you will need to setup the client part.
More details can be found here.

Transports

SignalR supports the following techniques for handling real-time communication (in order of graceful fallback):

SignalR automatically chooses the best transport method that is within the capabilities of the server and client.

Here are some features of SignalR for ASP.NET Core:

  • Handles connection management automatically.
  • Sends messages to all connected clients simultaneously. For example, a chat room.
  • Sends messages to specific clients or groups of clients.
  • Scales to handle increasing traffic.

The source is hosted in a SignalR repository on GitHub.

Connect to a hub

The interface for a HubConnection to Invoke a server side method is:

invoke<T = any>(methodName: string, ...args: any[]): Promise<T>;

Example:

The number of parameters must match the server side.

The following code example creates and starts a connection. The hub's name is case insensitive:

const connection = new signalR.HubConnectionBuilder()

.withUrl("/url")

.configureLogging(signalR.LogLevel.Information)

.build();

async function start() {

try {

await connection.start();

console.log("SignalR Connected.");

} catch (err) {

console.log(err);

setTimeout(start, 5000);

}

};

connection.onclose(start);

// Start the connection.

start();

The HubConnectionBuilder class creates a new builder for configuring the server connection.

The withUrl function configures the hub URL.

Url to use for calling the API:

https://api.skchase.com/checkout
Important

The client code must use an absolute URL instead of a relative URL.
In our case we should change .withUrl("/url") to .withUrl("https://api.skchase.com/checkout").

You will find more specific example for a Javascript client under the Tutorials sections.