...

/

Configuring SignalR Client

Configuring SignalR Client

Learn to configure the SignalR client.

Overview

Different SignalR client types have different settings. But they share some common settings. Also, in all of the client types, you can set some configurations only once when you create a connection object, while other options can be changed at any point.

We will start with an overview of the configuration options of a JavaScript client. We'll open our site.js file, which is located under the js folder of the wwwroot folder of the SignalRServer project.

-SignalRServer
-wwwroot
-js
-site.js
File directory

We can locate this file by writing the path in the terminal below:

cd SignalR-on-.NET-6---the-complete-guide/Chapter-07/Part-02/LearningSignalR/SignalRServer/wwwroot/js

We will find the statement where the connection object is instantiated and replace it with the following:

Press + to interact
const connection = new signalR.HubConnectionBuilder()
.withUrl("/learningHub", {
transport: signalR.HttpTransportType.WebSockets | signalR.HttpTransportType.LongPolling,
headers: { "Key": "value" },
accessTokenFactory: null,
logMessageContent: true,
skipNegotiation: false,
withCredentials: true,
timeout: 100000
})
.configureLogging(signalR.LogLevel.Information)
.build();

Configuring JavaScript client

Here, we have added several configuration options. Let’s go over each one of them.

  • transport: This option determines the transport mechanisms that the client supports. As on the server, you can add multiple transport mechanisms by using bitwise operators.

By default, the client would support all three transport mechanisms, prioritizing WebSocket.

  • headers: This option allows us to apply any custom HTTP headers to your request in the form of key-value pairs.

  • accesssTokenFactory: This setting allows us to apply an authentication token. We will cover it in more detail later.

  • logMessageContent: If set, this setting will log the content of the messages into the console.

  • skipNegotiation: If WebSocket transport is used, the negotiation can be skipped. Otherwise, this option will have no effect. Skipping negotiation will establish the connection faster.

  • withCredentials: If set, this option will apply credentials to the request. This is especially relevant to CORS requests.

  • timeout: This setting determines the maximum allowed time for HTTP requests. It’s not applied to poll requests of long polling, EventSource, or WebSocket.

Setting logging level

The configureLogging method allows us to set the minimum severity level at which messages will be logged. It can be either represented by a literal string value or a constant. When a particular severity level is set, events of that severity level and above are logged, but events of lower severity levels aren’t logged.

The available severity levels are as follows: