Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RealtimeManager

The realtime manager allows realtime publish and subscribe (pub/sub) messaging through websockets.

Realtime makes it possible to open a two-way interactive communication session between the user's device (e.g., browser, smartphone) and a server. With realtime, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

The configuration parameters of the realtime module is specified when creating the Altogic client library instance. In particular three key parameters affect how realtime messaging works in your apps.

  • echoMessages - This boolean parmeter enables or prevents realtime messages originating from this connection being echoed back on the same connection. By default messsages are echoed back.
  • bufferMessages - By default, any event emitted while the realtime socket is not connected will be buffered until reconnection. You can turn on/off the message buffering using this parameter. While enabiling this festure is useful in most cases (when the reconnection delay is short), it could result in a huge spike of events when the connection is restored.
  • autoJoinChannels - This parameter enables or disables automatic join to channels already subscribed in case of websocket reconnection. When websocket is disconnected, it automatically leaves subscribed channels. This parameter helps re-joining to already joined channels when the connection is restored. If this parameter is set to false, you need to listen to connect and disconnect events to manage your channel subscriptions.
export

Hierarchy

Index

Constructors

Properties

#autoJoinChannels: boolean

The flag to enable or prevent automatic join to channels already subscribed in case of websocket reconnection. When websocket is disconnected, it automatically leaves subscribed channels. This parameter helps re-joining to already joined channels when the connection is restored.

#bufferMessages: boolean

By default, any event emitted while the realtime socket is not connected will be buffered until reconnection. You can turn on/off the message buffering using this parameter.

#channels: Map<string, boolean>

Keeps the list of channels this socket is subscribed to. In case of a reconnect, if autoJoinChannels is enabled then joins to the list of channels specified in this map.

#echoMessages: boolean

The default setting whether to enable or prevent realtime messages originating from this connection being echoed back on the same connection.

#socket: any

The web socket object which is basically an EventEmitter which sends events to and receive events from the server over the network.

#userData: any

Keeps a reference to the latest user data that is updated using the update method.

fetcher: Fetcher

The http client to make RESTful API calls to the application's execution engine

Methods

  • #joinChannels(): void
  • In case of a reconnect, if autoJoinChannels is enabled in realtime configuration settings, then re-joins to the list of channels already subscribed.

    Returns void

  • broadcast(eventName: string, message: any, echo?: boolean): void
  • Sends the message identified by the eventName to all connected members of the app. All serializable datastructures are supported for the message, including Buffer.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    throws

    Throws an exception if eventName is not specified

    Parameters

    • eventName: string

      The name of the event.

    • message: any

      The message payload/contents.

    • Optional echo: boolean

      Override the echo flag specified when creating the websocket to enable or prevent realtime messages originating from this connection being echoed back on the same connection.

    Returns void

  • close(): void
  • Manually closes the realtime connection. In this case, the socket will not try to reconnect.

    Returns void

  • Returns the array of listener functions for the event named eventName.

    Parameters

    • eventName: string

      The name of the event.

    Returns ListenerFunction[]

    Array of listener functions

  • getMembers(channel: string): Promise<MemberData[]>
  • Returns the members of the specified channel.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    throws

    Throws an exception if channel is not specified

    Parameters

    • channel: string

      The name of the channel.

    Returns Promise<MemberData[]>

    Returns array of channel member data. If no channel members then returns and empty array []

  • getSocketId(): string
  • Returns the unique identifier of the underlying websocket

    Returns string

    The socket id

  • isConnected(): boolean
  • Returns true if the realtime socket is connected otherwise false

    Returns boolean

  • join(channel: string, echo?: boolean): void
  • Adds the realtime socket to the specified channel. As a result of this action a channel:join event is sent to all members of the channel notifying the new member arrival.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    throws

    Throws an exception if channel is not specified

    Parameters

    • channel: string

      The name of the channel.

    • Optional echo: boolean

      Override the echo flag specified when creating the websocket to enable or prevent channel:join event originating from this connection being echoed back on the same connection.

    Returns void

  • leave(channel: string, echo?: boolean): void
  • Removes the realtime socket from the specified channel. As a result of this action a channel:leave event is sent to all members of the channel notifying the departure of existing member.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    throws

    Throws an exception if channel is not specified

    Parameters

    • channel: string

      The name of the channel.

    • Optional echo: boolean

      Override the echo flag specified when creating the websocket to enable or prevent channel:leave event originating from this connection being echoed back on the same connection.

    Returns void

  • Removes the specified listener function from the listener array for the event named eventName.

    If listener is not specified, it removes all listeners for for the event named eventName.

    If neither eventName nor listener is specified, it removes all listeners for all events.

    Parameters

    • Optional eventName: string

      The name of the event.

    • Optional listener: ListenerFunction

      The listener function.

    Returns void

  • Removes the previously registered listener function. If no listener is provided, all catch-all listener functions are removed.

    Parameters

    Returns void

  • Registers a new catch-all listener function. This listener function is triggered for all messages sent to this socket.

    Parameters

    Returns void

  • onConnect(listener: (() => void)): void
  • Callback function fired upon successfull realtime connection, including a successful reconnection.

    Parameters

    • listener: (() => void)

      The listener function.

        • (): void
        • Returns void

    Returns void

  • onDisconnect(listener: ((reason: string) => void)): void
  • Callback function fired upon realtime disconnection. Passes the diconnection reason as a string parameter to the callback function.

    Parameters

    • listener: ((reason: string) => void)

      The listener function.

        • (reason: string): void
        • Parameters

          • reason: string

          Returns void

    Returns void

  • onError(listener: ((error: any) => void)): void
  • Callback function fired upon a realtime connection error. Passes the error as a parameter to the callback function.

    Parameters

    • listener: ((error: any) => void)

      The listener function.

        • (error: any): void
        • Parameters

          • error: any

          Returns void

    Returns void

  • Convenience method which registers a new listener function for channel:join events which are emitted when a new member joins a channel.

    Parameters

    Returns void

  • Convenience method which registers a new listener function for channel:leave events which are emitted when an existing member leaves a channel.

    Parameters

    Returns void

  • onReconnectAttempt(listener: ((attemptNumber: number) => void)): void
  • Callback function fired upon an attempt to reconnect. Passes the reconnection attempt number as a parameter to the callback function.

    Parameters

    • listener: ((attemptNumber: number) => void)

      The listener function.

        • (attemptNumber: number): void
        • Parameters

          • attemptNumber: number

          Returns void

    Returns void

  • Convenience method which registers a new listener function for channel:update events which are emitted when a channel member updates its member data.

    Parameters

    Returns void

  • Registers a method to listen to main user events. The following events will be listened:

    Event Description
    user:signin Triggered whenever a new user session is created.
    user:signout Triggered when a user session is deleted. If AuthManager.signOutAll or AuthManager.signOutAllExceptCurrent method is called then for each deleted sesssion a separate user:signout event is triggered.
    user:update Triggered whenever user data changes including password, email and phone number updates.
    user:delete Triggered when the user data is deleted from the database.
    user:pwdchange Triggered when the user password changes, either through direct password update or password reset.
    user:emailchange Triggered whenever the email of the user changes.
    user:phonechange Triggered whenever the phone number of the user changes.

    Please note that user:update and user:delete events are fired only when a specific user with a known _id is updated or deleted in the database. For bulk user update or delete operations these events are not fired.

    Parameters

    • listener: UserEventListenerFunction

      The listener function. This function gets two input parameters the name of the event that is being triggered and the user session object that has triggered the event. If the event is triggered by the user without a session, then the session value will be null.

    Returns void

  • Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

    Parameters

    • eventName: string

      The name of the event.

    • listener: ListenerFunction

      The listener function.

    Returns void

  • open(): void
  • Manually open the realtime connection, connects the socket.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    Returns void

  • send(channel: string, eventName: string, message: any, echo?: boolean): void
  • Sends the message identified by the eventName to the provided channel members only. All serializable datastructures are supported for the message, including Buffer.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    throws

    Throws an exception if channel or eventName is not specified

    Parameters

    • channel: string

      The name of the channel.

    • eventName: string

      The name of the event.

    • message: any

      The message payload/contents.

    • Optional echo: boolean

      Override the echo flag specified when creating the websocket to enable or prevent realtime messages originating from this connection being echoed back on the same connection.

    Returns void

  • updateProfile(data: any, echo?: boolean): void
  • Update the current realtime socket member data and broadcast an update event to each joined channel so that other channel members can get the information about the updated member data. Whenever the socket joins a new channel, this updated member data will be broadcasted to channel members. As a result of this action a channel:update event is sent to all members of the subscribed channels notifying the member data update.

    As an example if you are developing a realtime chat application it might be a good idea to store the username and user profile picture URL in member data so that joined chat channels can get updated user information.

    If the client library key is set to enforce session, an active user session is required (e.g., user needs to be logged in) to call this method.

    Parameters

    • data: any

      data payload for the current member. The supported payload types are Strings, JSON objects and arrays, buffers containing arbitrary binary data, and null.

    • Optional echo: boolean

      Override the echo flag specified when creating the websocket to enable or prevent channel:update event originating from this connection being echoed back on the same connection.

    Returns void

Generated using TypeDoc