GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Close
  • FAQ
  • Docs
  • API
  • Community
  • Blog
  • Accessing ReQL
  • r
  • connect
  • close
  • reconnect
  • use
  • run
  • changes

This view

  • Original page
  • New address

GeezerVison is showing rethinkdb.com — ReQL command reference - RethinkDB


▲ Back to top

Command syntax

cursor.addListener(event, listener)

cursor.on(event, listener)

cursor.once(event, listener)

cursor.removeListener(event, listener)

cursor.removeAllListeners([event])

cursor.setMaxListeners(n)

cursor.listeners(event)

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

Description

Cursors and feeds implement the same interface as Node’s EventEmitter.

  • Two events can be emitted, data and error.
  • Once you start using the EventEmitter interface, the other RethinkDB cursor commands like next, toArray, and each will not be available anymore.
  • The first time you call an EventEmitter method, the cursor or feed will emit data just after the I/O events callbacks and before setTimeout and setInterval callbacks.

Example: Broadcast all messages with socket.io.

r.table("messages").orderBy({index: "date"}).run(conn, function(err, cursor) {
    if (err) {
        // Handle error
        return
    }

    cursor.on("error", function(error) {
        // Handle error
    })
    cursor.on("data", function(message) {
        socket.broadcast.emit("message", message)
    })
});

This query can be rewritten with the each command:

r.table("messages").orderBy({index: "date"}).run(conn, function(err, cursor) {
    if (err) {
        // Handle error
        return
    }
    
    cursor.each(function(error, message) {
        if(error) {
            // Handle error
        }
        socket.broadcast.emit("message", message)
    })
});

Example: Broadcast all the messages inserted.

r.table("messages").changes().filter({old_val: null}).run(conn, function(err, feed) {
    if (err) {
        // Handle error
        return
    }

    feed.on("error", function(error) {
        // Handle error
    })
    feed.on("data", function(newMessage) {
        socket.broadcast.emit("message", newMessage)
    })
});

Related commands

  • next
  • each

Get more help

Couldn't find what you were looking for?

  • Ask a question on Stack Overflow
  • Chat with us and our community on Slack
  • Talk to the team on IRC on #[email protected] — via Webchat
  • Ping @rethinkdb on Twitter
  • Post an issue on the documentation issue tracker on GitHub

Contribute: edit this page or open an issue

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.