Skip to main content

WebSocketProvider <API>

Use WebSocketProvider to connect to a Node using a WebSocket connection, i.e. over the ws or wss protocol.

@example
const provider = new WebSocketProvider(
`ws://localhost:8545`,
{
headers: {
// to provide the API key if the Node requires the key to be inside the `headers` for example:
'x-api-key': '<Api key>',
},
},
{
delay: 500,
autoReconnect: true,
maxAttempts: 10,
},
);

The second and the third parameters are both optional. And you can for example, the second parameter could be an empty object or undefined. *

@example
const provider = new WebSocketProvider(
`ws://localhost:8545`,
{},
{
delay: 500,
autoReconnect: true,
maxAttempts: 10,
},
);

Hierarchy

  • SocketProvider<WebSocket.MessageEvent, WebSocket.CloseEvent, WebSocket.ErrorEvent, API>
    • WebSocketProvider

Index

Constructors

publicconstructor

  • new WebSocketProvider<API>(socketPath: string, socketOptions?: ClientOptions | ClientRequestArgs, reconnectOptions?: Partial<ReconnectOptions>): default<API>
  • This is a class used for Web Socket connections. It extends the abstract class SocketProvider SocketProvider that extends the EIP-1193 provider EIP1193Provider.


    Type parameters

    Parameters

    • socketPath: string

      The path to the Web Socket.

    • optionalsocketOptions: ClientOptions | ClientRequestArgs

      The options for the Web Socket client.

    • optionalreconnectOptions: Partial<ReconnectOptions>

      The options for the socket reconnection ReconnectOptions

    Returns default<API>

Accessors

SocketConnection

  • get SocketConnection(): unknown
  • Returns unknown

[symbol]

  • get [symbol](): boolean
  • Returns boolean

Methods

connect

  • connect(): void
  • Try to establish a connection to the socket


    Returns void

disconnect

  • disconnect(code?: number, data?: string): void
  • Disconnects the socket


    Parameters

    • optionalcode: number

      The code to be sent to the server

    • optionaldata: string

      The data to be sent to the server

    Returns void

publicgetStatus

on

  • Registers a listener for the specified event type.


    Parameters

    Returns void

once

  • Registers a listener for the specified event type that will be invoked at most once.


    Parameters

    Returns void

removeAllListeners

  • removeAllListeners(type: string): void
  • Removes all listeners for the specified event type.


    Parameters

    • type: string

      The event type to remove the listeners for

    Returns void

removeListener

  • Removes a listener for the specified event type.


    Parameters

    Returns void

request

reset

  • reset(): void
  • Resets the socket, removing all listeners and pending requests


    Returns void

send

  • @deprecated

    Please use .request instead.


    Type parameters

    Parameters

    Returns void

sendAsync

supportsSubscriptions

  • supportsSubscriptions(): boolean

  • Returns boolean

    true if the socket supports subscriptions

staticisWeb3Provider

  • isWeb3Provider(provider: unknown): boolean
  • Parameters

    • provider: unknown

    Returns boolean