AdonisJS API
    Preparing search index...

    Class AbstractConnection<T, Events>Abstract

    Abstract factory implements the shared functionality required by Redis cluster and the normal Redis connections.

    class MyConnection extends AbstractConnection<Redis, MyEvents> {
    protected makeSubscriberConnection() {
    // Implementation specific to your connection type
    }
    }

    Type Parameters

    Hierarchy (View Summary)

    Index
    ioConnection: T

    Reference to the main ioRedis connection

    ioSubscriberConnection?: T

    Reference to the main ioRedis subscriber connection

    lastError?: any

    The last error emitted by the error event. We set it to null after the ready event

    connectionName: string

    Name identifier for this connection

    • get status(): | "connect"
      | "wait"
      | "end"
      | "close"
      | "connecting"
      | "ready"
      | "reconnecting"
      | "disconnecting"

      Returns status of the main connection

      Returns
          | "connect"
          | "wait"
          | "end"
          | "close"
          | "connecting"
          | "ready"
          | "reconnecting"
          | "disconnecting"

    • get subscriberStatus(): | "connect"
      | "wait"
      | "end"
      | "close"
      | "connecting"
      | "ready"
      | "reconnecting"
      | "disconnecting"
      | undefined

      Returns status of the subscriber connection or undefined when there is no subscriber connection

      Returns
          | "connect"
          | "wait"
          | "end"
          | "close"
          | "connecting"
          | "ready"
          | "reconnecting"
          | "disconnecting"
          | undefined

    • get autoPipelineQueueSize(): number

      Get the number of commands queued in automatic pipelines. This is not available (and returns 0) until the cluster is connected and slots information have been received.

      Returns number

    • Returns a boolean notifying if the connection is in connecting state

      Returns boolean

      if (connection.isConnecting()) {
      console.log('Connection is establishing...')
      }
    • Returns a boolean notifying if the connection is in ready state

      Returns boolean

      if (connection.isReady()) {
      await connection.ioConnection.set('key', 'value')
      }
    • Returns a boolean notifying if the connection has been closed

      Returns boolean

      if (connection.isClosed()) {
      console.log('Connection is closed')
      }
    • Subscribe to a given channel to receive Redis pub/sub events. A new subscriber connection will be created/managed automatically.

      Parameters

      • channel: string

        The channel name to subscribe to

      • handler: PubSubChannelHandler

        Function to handle received messages

      • Optionaloptions: PubSubOptions

        Optional subscription configuration

      Returns void

      connection.subscribe('notifications', (message) => {
      console.log('Received:', message)
      })
    • Publish the pub/sub message

      Parameters

      • channel: string

        The channel to publish to

      • message: string

        The message to publish

      • callback: (error: Error | null | undefined, count: number | undefined) => void

        Optional callback for completion

      Returns void

      // Promise-based
      const count = await connection.publish('notifications', 'Hello World')

      // Callback-based
      connection.publish('notifications', 'Hello World', (err, count) => {
      console.log('Published to', count, 'subscribers')
      })
    • Publish the pub/sub message

      Parameters

      • channel: string

        The channel to publish to

      • message: string

        The message to publish

      Returns Promise<number>

      // Promise-based
      const count = await connection.publish('notifications', 'Hello World')

      // Callback-based
      connection.publish('notifications', 'Hello World', (err, count) => {
      console.log('Published to', count, 'subscribers')
      })
    • Define a custom command using LUA script. You can run the registered command using the "runCommand" method.

      Parameters

      • ...args: [name: string, definition: {}]

        Arguments for defining the command

      Returns this

      connection.defineCommand('myCommand', {
      numberOfKeys: 1,
      lua: 'return redis.call("get", KEYS[1])'
      })
    • Run a pre registered command

      Parameters

      • command: string

        The name of the registered command

      • ...args: any[]

        Arguments to pass to the command

      Returns any

      const result = await connection.runCommand('myCommand', 'key1')