EventEmitter (connection)

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();

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/javascript/event_emitter/

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部