Back to top

ReQL command: EventEmitter (connection)

Command syntax

connection.addListener(event, listener)

connection.on(event, listener)

connection.once(event, listener)

connection.removeListener(event, listener)

connection.removeAllListeners([event])

connection.setMaxListeners(n)

connection.listeners(event)

connection.emit(event, [arg1], [arg2], [...])

Description

Connections implement the same interface as Node’s EventEmitter. This allows you to listen for changes in connection state.

Four events are emitted: connect, close, timeout and error.

  • connect: a successful connection to the server.
  • close: the connection has been closed, either through an error or by calling connection.close().
  • timeout: the underlying socket has timed out.
  • error: a protocol-level error has occurred. (This will not be sent on a query error; those are returned to callbacks/promises.)

Example: Monitor the connection state with events.

r.connect({}, function(err, conn) {
    if (err) throw err;

    conn.addListener('error', function(e) {
        processNetworkError(e);
    });

    conn.addListener('close', function() {
        cleanup();
    });

    runQueries(conn);
});

Example: As in Node, on is a synonym for addListener.

conn.on('close', function() {
    cleanup();
});
conn.close();

Get more help

Couldn't find what you were looking for?