phoenix-websocket
    Preparing search index...

    Class PhoenixWebsocket

    Represents a connection instance to a Phoenix Sockets endpoint.

    Index

    Constructors

    • Parameters

      • url: string

        The fully qualified url to your Phoenix socket endpoint.

      • OptionalqueryParams: { [key: string]: string }

        Optional object of key/value pairs to be appended to the url as query parameters.

      • OptionaltimeoutInMs: number

        Optional timeout value in milliseconds, if not provided the default 60 second timeout will be used.

      Returns PhoenixWebsocket

    Properties

    onConnectedCallback: undefined | (() => void)

    A callback to be called whenever the WebSocket successfully connects or reconnects.

    onDisconnectedCallback: undefined | (() => void)

    A callback to be called whenever the WebSocket is disconnected.

    Accessors

    • get subscribedTopics(): string[]

      An array of all the topics you are currently successfully connected to. Does not include topics that have been disconnected, or that are in the process of connecting.

      Returns string[]

    Methods

    • Attempt to open the websocket connection.

      Returns Promise<void>

      A promise which will resolve when the connection is successfully opened. If the websocket is already connected, this will resolve immediately.

    • Close the current websocket connection.

      Parameters

      • OptionalclearTopics: boolean = true

        If true, all topics will be unsubscribed from and removed as part of the disconnection. If false, the topics will remain subscribed to and will automatically be resubscribed to if the socket is reconnected.

      Returns void

    • Disconnects the websocket if it isn't already, and then cleans up event listeners. This will also be called by PhoenixWebsocket's Symbol.dispose() if the using keyword is preferred over explicitly calling dispose().

      Returns void

    • Send a message and optional payload to the given topic.

      Parameters

      • topic: string

        The topic to send the message to. You must already be subscribed to this topic a topic before you can send a message to it.

      • message: string

        The message to send.

      • Optionalpayload: { [key: string]: any }

        An optional payload to be sent along with the message.

      Returns Promise<PhoenixReply>

      A promise which will resolve with the reply sent from the server.

    • Set the log level for this PhoenixWebsocket instance. If you would like nothing to be logged to the browser console, you can set this to PhoenixWebsocketLogLevels.Quiet.

      When unchanged, this will log information connection and subscription status to the console by default.

      Parameters

      Returns void

    • Subscribe to the passed in topic. This will attempt to subscribe immediately if the websocket connection is already open, otherwise it will try to subscribe once the connection is opened.

      Optionally pass in a topicMessageHandler to handle incoming messages for the topic. If this is left undefined, then it is assumed you will only interact with the topic via messages and responses.

      If an 'error' status is received from the server in response to the join request, then the promise will be reject with the payload of the error response.

      Parameters

      • topic: string

        The topic to subscribe to.

      • Optionalpayload: { [key: string]: any }

        An optional payload object to be sent along with the join request.

      • OptionalmessageHandlers: { [message: string]: TopicMessageHandler }

        An optional object containing mappings of messages to message handler callbacks, which are called when the given message is received.

      • OptionalreconnectHandler: (reconnectPromise: Promise<void>) => void

        An optional callback which will be called with the connection promise of any subsequent reconnection attempts (useful when, for example, you expect that a topic connection may or may not result in error, and you want to implement custom logic on these errors). Specifically will not be called with the initial connection Promise returned by this method.

      Returns Promise<void>

    • Subscribe to the passed in topic. This will attempt to subscribe immediately if the websocket connection is already open, otherwise it will try to subscribe once the connection is opened.

      Optionally pass in a topicMessageHandler to handle incoming messages for the topic. If this is left undefined, then it is assumed you will only interact with the topic via messages and responses.

      If an 'error' status is received from the server in response to the join request, then the promise will be reject with the payload of the error response.

      Parameters

      • topic: string

        The topic to subscribe to.

      • Optionalpayload: { [key: string]: any }

        An optional payload object to be sent along with the join request.

      • OptionalmessageHandlers: Map<string, TopicMessageHandler>

        An optional object containing mappings of messages to message handler callbacks, which are called when the given message is received.

      • OptionalreconnectHandler: (reconnectPromise: Promise<void>) => void

        An optional callback which will be called with the connection promise of any subsequent reconnection attempts (useful when, for example, you expect that a topic connection may or may not result in error, and you want to implement custom logic on these errors). Specifically will not be called with the initial connection Promise returned by this method.

      Returns Promise<void>

    • Unsubscribe from the passed in topic.

      Parameters

      • topic: string

        The topic to unsubscribe from.

      Returns void