HubConnection class

Represents a connection to a SignalR Hub.

Properties

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).

Methods

invoke

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

Removes all handlers for the specified hub method.

off

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on. Passing a different instance (even if the function body is the same) will not remove the handler.

on

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

onclose

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

send

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

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

Property Details

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).

public serverTimeoutInMilliseconds: number

Property Value

Method Details

invoke

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

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

Removes the specified handler for the specified hub method. You must pass the exact same Function instance as was previously passed to on. 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.

on

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

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).

send

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

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