Events

Your application can listen to events for when data changes in your application.

Data changes

Once a datastore or database is opened, you can listen to changes to automatically update your application:

const filter = {
    status: 'active'
}

const listener = await database.changes(function(row) {
  console.log("Row changed", row)
}, filter)

Try this out with your own profile data in the browser in our tutorial.

Options:

  • filter: An optional JSON object. The listener will only raise events if the data in the object matches the supplied filter.

You can cancel the event listener:

listener.cancel()

Similarly you can call:

const listener = await datastore.changes(function(row) { ... })

Inbox messages

You can be notified when a new inbox message arrives for your application:

const messaging = await context.getMessaging()
messaging.onMessage(function(message) { console.log('New message!', message)})

Database sync changes

Data is automatically synchronized from remote encrypted servers to the local client. It’s possible to listen to events related to this syncing behavior:

const listener = database.onSync('error', (err) => { console.log(err) })
listener.cancel()

A full list of events is available via the PouchDB Documentation

Last updated