Skip to content

Subscribe

SKDB allows you to create reactive applications which instantly respond to incoming changes.

Subscribe to changes API

watch

  • Purpose: To subscribe to real-time data changes. The provided callback will be invoked every time the query result changes, with all of the rows of the query result as its argument.

  • Usage:

    localDb.watch("SELECT * FROM users WHERE userID = 150", parameters, callback);
    

  • Imports:
    import type { SKDB } from 'skdb'
    
  • Recommendation: Avoid using watch when programming in the browser; prefer more integrated solutions (such as useQuery) from the skdb-react package instead. watch is typically going to be useful when developing Node services.

watchChanges

  • Purpose: Similar to watch, but the callback is invoked only on the changes, as opposed to the full result set. The first parameter to callback contains the rows that have been added, and the second one contains the rows that have been removed, since the preceding call to callback.
  • Usage:
    localDb.watchChanges(query, parameters, callback);
    
  • Imports:
    import type { SKDB } from 'skdb'
    
  • Example:
    localDb.watchChanges(
      "SELECT id, author, body FROM messages where t > @now",
      { now: Date.now() },
      (added: any[], removed: any[]) => {
        added.forEach(msg => console.log(`New message: ${msg.body}`));
        removed.forEach(msg => console.log(`${msg.author} deleted ${msg.id}`));
      }
    );