pg-rx-listen API
    Preparing search index...

    Class PgListenConnection

    A class that represents a single connection to a PostgreSQL database and provides methods for listening to and sending notifications.

    Index

    Constructors

    Properties

    onConnect: Observable<IConnectParams>

    Emits when a new connection has been established.

    onDisconnect: Observable<IDisconnectParams>

    Emits when a connection has been lost (temporarily), and is due for automatic reconnection.

    onEnd: Observable<any>

    Emits when the connection has been lost permanently.

    After this event, all LISTEN subscribers have received the error event, and the class is no longer usable.

    This is for monitoring / logging purposes.

    onQuery: Observable<string>

    Emits all SQL queries being executed.

    This is for monitoring / logging purposes.

    Accessors

    • get isConnected(): boolean

      Returns true if the connection is currently active.

      Returns boolean

    • get isLive(): boolean

      Returns true if the connection is either active or being established.

      Returns false when the connection is lost permanently, and onEnd has been emitted.

      Returns boolean

    • get liveChannels(): string[]

      List of channels that are currently being listened to.

      Returns string[]

    Methods

    • Creates an observable that emits LISTEN notifications for the specified channels.

      Note that it does not trigger a connection with listening. That happens only after you subscribe to the returned observable.

      Parameters

      • channels: string[]

        List of channels to listen to.

      • Optionalready: () => void

        Optional callback for when connection has been established, and queries finished execution. It is re-sent when the connection changes or new subscribers appear.

      Returns Observable<INotificationMessage>

      An observable that emits LISTEN notifications for the specified channels.

    • Sends a notification into the specified channels, with optional payload.

      Parameters

      • channels: string[]

        List of channels to notify.

      • Optionalpayload: string

        Optional payload to send with the notification.

      Returns Promise<boolean>

      true if the notification was sent successfully, false otherwise.

      // "ls" is of type "PgListenConnection"
      ls.listen(['channel_1', 'channel_2'], async () => {
      await ls.notify(['channel_1', 'channel_2'], 'Hello World!');
      })
      .subscribe(msg => {
      console.log(msg);
      });

      Output:

      {
      channel: 'channel_1',
      length: 31,
      payload: 'Hello World!',
      processId: 644
      }
      {
      channel: 'channel_2',
      length: 31,
      payload: 'Hello World!',
      processId: 644
      }