pg-listener API
    Preparing search index...

    Interface IListenResult

    Result from PgListener.listen method.

    interface IListenResult {
        add: (channels: string[]) => Promise<string[]>;
        cancel: (unlisten?: boolean) => Promise<boolean>;
        createIterable: () => AsyncIterable<IListenMessage>;
        isConnected: boolean;
        isLive: boolean;
        notify: (channels: string[], payload?: string) => Promise<boolean>;
        remove: (channels: string[]) => Promise<string[]>;
    }
    Index

    Properties

    add: (channels: string[]) => Promise<string[]>

    Adds a list of channels to listen to and executes LISTEN on those, if currently connected.

    It will ignore channels that are already on the list.

    Type Declaration

      • (channels: string[]): Promise<string[]>
      • Parameters

        • channels: string[]

          List of channels to be added.

        Returns Promise<string[]>

        A promise that resolves to a list of channels actually added (not on the list yet).

    remove

    cancel: (unlisten?: boolean) => Promise<boolean>

    Closes the connection allocated by PgListener.listen method, with optional UNLISTEN request for all channels.

    If successful, it removes the connection from PgListener.connections list, and returns true.

    It also ends all iterables created by createIterable.

    Type Declaration

      • (unlisten?: boolean): Promise<boolean>
      • Parameters

        • Optionalunlisten: boolean

          Optional flag indicating whether to also execute UNLISTEN for all channels.

        Returns Promise<boolean>

        A promise that resolves to a boolean indicating whether the cancellation was successful.

    createIterable: () => AsyncIterable<IListenMessage>

    Creates an asynchronous iterable object to receive notification messages.

    The iteration is infinite, unless you explicitly call cancel, or the connection is lost permanently.

    The iterable object never throws any error. Even if connection is lost permanently, it just will end the iteration, and that's it, same as when calling cancel.

    Type Declaration

    const result = await ls.listen(['channel_1', 'channel_2']);

    for await (const msg of result.createIterable()) {
    console.log(msg);
    }
    isConnected: boolean

    Checks if the connection object allocated by PgListener.listen is currently in the connected state.

    isLive: boolean

    Checks if the connection object allocated by PgListener.listen is currently in the connected state or trying to connect/reconnect, i.e. if the connection is still generally alive.

    When false, the connection is lost permanently, and event onFailedReconnect has been triggered.

    notify: (channels: string[], payload?: string) => Promise<boolean>

    Sends a notification to the list of specified channels, on the connection allocated by PgListener.listen method.

    Type Declaration

      • (channels: string[], payload?: string): Promise<boolean>
      • Parameters

        • channels: string[]

          An array of destination channels where the notification should be sent. If you pass in an empty list, the method will do nothing and just return false.

        • Optionalpayload: string

          Optional payload data to include in the notification.

        Returns Promise<boolean>

        A promise that resolves to a boolean indicating the success of notification.

    remove: (channels: string[]) => Promise<string[]>

    Removes a list of channels from being listened to and executes UNLISTEN on those, if currently connected.

    It will ignore channels that are not on the list.

    If no more channels are left after the removal, the connection is NOT closed.

    Type Declaration

      • (channels: string[]): Promise<string[]>
      • Parameters

        • channels: string[]

          List of channels to be removed.

        Returns Promise<string[]>

        A promise that resolves to a list of channels actually removed (those still on the list).

    add