Class PhoenixWebsocket

Represents a connection instance to a Phoenix Sockets endpoint.

Hierarchy

  • PhoenixWebsocket

Constructors

  • Parameters

    • url: string

      The fully qualified url to your Phoenix socket endpoint.

    • Optional queryParams: {
          [key: string]: string;
      }

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

      • [key: string]: string
    • Optional timeoutInMs: number

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

    Returns PhoenixWebsocket

Properties

HEARTBEAT_INTERVAL: 30000 = 30000
TIMEOUT_LENGTH: number = ...
TIMEOUT_THRESHOLD: 3 = 3
_connectionStatus: WebsocketStatuses = WebsocketStatuses.Disconnected
_nextMessageId: number = 1
connectionAttempt: number = 0
heartbeatTimeout: undefined | number
logLevel: PhoenixWebsocketLogLevels = PhoenixWebsocketLogLevels.Warnings
onConnectedCallback: undefined | (() => void)

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

Type declaration

    • (): void
    • Returns void

onConnectedResolvers: (() => void)[] = []

Type declaration

    • (): void
    • Returns void

onDisconnectedCallback: undefined | (() => void)

A callback to be called whenever the WebSocket is disconnected.

Type declaration

    • (): void
    • Returns void

phoenixReplyQueue: Map<string, ReplyQueueEntry> = ...
reconnectionTimeout: undefined | number
socket?: WebSocket
topics: Map<string, PhoenixTopic> = ...
wsUrl: string

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

    • Optional clearTopics: 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

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

    • Optional payload: {
          [key: string]: any;
      }

      An optional payload to be sent along with the message.

      • [key: string]: any

    Returns Promise<PhoenixReply>

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

    Throws

    Throws

    Throws

    Throws

    Throws

    Throws

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

    • Optional payload: {
          [key: string]: any;
      }

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

      • [key: string]: any
    • Optional messageHandlers: {
          [message: string]: TopicMessageHandler;
      }

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

    Returns Promise<void>

    Throws

    Throws

    Throws

    Throws

  • Parameters

    • topic: string
    • Optional payload: {
          [key: string]: any;
      }
      • [key: string]: any
    • Optional messageHandlers: Map<string, TopicMessageHandler>

    Returns Promise<void>

  • Unsubscribe from the passed in topic.

    Parameters

    • topic: string

      The topic to unsubscribe from.

    Returns void

Generated using TypeDoc