HubConnection class

Represents a connection to a SignalR Hub.

Properties

baseUrl

Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or Reconnecting states.

connectionId

Represents the connection id of the <xref:HubConnection> on the server. The connection id will be null when the connection is either in the disconnected state or if the negotiation step was skipped.

keepAliveIntervalInMilliseconds

Default interval at which to ping the server. The default value is 15,000 milliseconds (15 seconds). Allows the server to detect hard disconnects (like when a client unplugs their computer).

serverTimeoutInMilliseconds

The server timeout in milliseconds. If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error. The default timeout value is 30,000 milliseconds (30 seconds).

state

Indicates the state of the <xref:HubConnection> to the server.

Methods

invoke(string, any[])

Invokes a hub method on the server using the specified name and arguments. The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of resolving the Promise.

off(string)

Removes all handlers for the specified hub method.

off(string, (args: any[]) => void)

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on(string, (args: any[]) => void). Passing a different instance (even if the function body is the same) will not remove the handler.

on(string, (args: any[]) => void)

Registers a handler that will be invoked when the hub method with the specified method name is invoked.

onclose((error?: Error) => void)

Registers a handler that will be invoked when the connection is closed.

onreconnected((connectionId?: string) => void)

Registers a handler that will be invoked when the connection successfully reconnects.

onreconnecting((error?: Error) => void)

Registers a handler that will be invoked when the connection starts reconnecting.

send(string, any[])

Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still be processing the invocation.

start()

Starts the connection.

stop()

Stops the connection.

stream(string, any[])

Invokes a streaming hub method on the server using the specified name and arguments.

Property Details

baseUrl

Sets a new url for the HubConnection. Note that the url can only be changed when the connection is in either the Disconnected or Reconnecting states.

baseUrl: string

Property Value

string

connectionId

Represents the connection id of the <xref:HubConnection> on the server. The connection id will be null when the connection is either in the disconnected state or if the negotiation step was skipped.

connectionId: string | null

Property Value

string | null

keepAliveIntervalInMilliseconds

Default interval at which to ping the server. The default value is 15,000 milliseconds (15 seconds). Allows the server to detect hard disconnects (like when a client unplugs their computer).

keepAliveIntervalInMilliseconds: number

Property Value

number

serverTimeoutInMilliseconds

The server timeout in milliseconds. If this timeout elapses without receiving any messages from the server, the connection will be terminated with an error. The default timeout value is 30,000 milliseconds (30 seconds).

serverTimeoutInMilliseconds: number

Property Value

number

state

Indicates the state of the <xref:HubConnection> to the server.

state: HubConnectionState

Property Value

Method Details

invoke(string, any[])

Invokes a hub method on the server using the specified name and arguments. The Promise returned by this method resolves when the server indicates it has finished invoking the method. When the promise resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of resolving the Promise.

function invoke<T>(methodName: string, args: any[])

Parameters

methodName
string

The name of the server method to invoke.

args
any[]

The arguments used to invoke the server method.

Returns

Promise<T>

off(string)

Removes all handlers for the specified hub method.

function off(methodName: string)

Parameters

methodName
string

The name of the method to remove handlers for.

off(string, (args: any[]) => void)

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on(string, (args: any[]) => void). Passing a different instance (even if the function body is the same) will not remove the handler.

function off(methodName: string, method: (args: any[]) => void)

Parameters

methodName
string

The name of the method to remove handlers for.

method
(args: any) => void[]

The handler to remove. This must be the same Function instance as the one passed to on(string, (args: any[]) => void).

on(string, (args: any[]) => void)

Registers a handler that will be invoked when the hub method with the specified method name is invoked.

function on(methodName: string, newMethod: (args: any[]) => void)

Parameters

methodName
string

The name of the hub method to define.

newMethod
(args: any) => void[]

The handler that will be raised when the hub method is invoked.

onclose((error?: Error) => void)

Registers a handler that will be invoked when the connection is closed.

function onclose(callback: (error?: Error) => void)

Parameters

callback
(error?: Error) => void

The handler that will be invoked when the connection is closed. Optionally receives a single argument containing the error that caused the connection to close (if any).

onreconnected((connectionId?: string) => void)

Registers a handler that will be invoked when the connection successfully reconnects.

function onreconnected(callback: (connectionId?: string) => void)

Parameters

callback
(connectionId?: string) => void

The handler that will be invoked when the connection successfully reconnects.

onreconnecting((error?: Error) => void)

Registers a handler that will be invoked when the connection starts reconnecting.

function onreconnecting(callback: (error?: Error) => void)

Parameters

callback
(error?: Error) => void

The handler that will be invoked when the connection starts reconnecting. Optionally receives a single argument containing the error that caused the connection to start reconnecting (if any).

send(string, any[])

Invokes a hub method on the server using the specified name and arguments. Does not wait for a response from the receiver. The Promise returned by this method resolves when the client has sent the invocation to the server. The server may still be processing the invocation.

function send(methodName: string, args: any[])

Parameters

methodName
string

The name of the server method to invoke.

args
any[]

The arguments used to invoke the server method.

Returns

Promise<void>

start()

Starts the connection.

function start()

Returns

Promise<void>

stop()

Stops the connection.

function stop()

Returns

Promise<void>

stream(string, any[])

Invokes a streaming hub method on the server using the specified name and arguments.

function stream<T>(methodName: string, args: any[])

Parameters

methodName
string

The name of the server method to invoke.

args
any[]

The arguments used to invoke the server method.

Returns