Firebase Connector
reshuffle-firebase-connector
Code | npm | Code sample
npm install reshuffle-firebase-connector
Reshuffle Firebase Connector
This package contains a Reshuffle connector to Google Firebase.
The following example tracks values in the Firebase realtime database:
const { Reshuffle } = require('reshuffle')
const { FirebaseConnector } = require('reshuffle-firebase-connector')
const app = new Reshuffle()
const fb = new FirebaseConnector(app, {
credentials: process.env.FIREBASE_CREDENTIALS,
databaseURL: process.env.FIREBASE_DATABASE_URL,
})
const ref = fb.ref('myKey')
fb.on({ type: 'Value', ref }, (snapshot) => {
console.log(`New value for key ${snapshot.key}: ${snapshot.val()}`)
})
app.start(8000)
setInterval(() => ref.set(Date.now()), 1000)
Table of Contents
Configuration Configuration options
Types TypeScript types
Connector events:
ChildAdded Child added to key
ChildChanged Child changed
ChildMoved Child moved
ChildRemoved Child removed
Value Value changed
Connector actions:
database Get the realtime database
ref Get a key reference in the realtime database
Configuration options
const app = new Reshuffle()
const firebaseConnector = new FirebaseConnector(app, {
credentials: process.env.FIREBASE_CREDENTIALS,
databaseURL: process.env.FIREBASE_DATABASE_URL,
})
Credentials can be an object or a JSON string with service account
credentials that can be downloaded from the Firebase admin console.
See the Credentials
interface exported from the connector for details.
The databaseURL
is a string provided by Firebase.
TypeScript types
The following types are exported from the connector:
- interface FirebaseCredentials Google service account credentials
- interface FirebaseConnectorOptions Connector configuration options
- type FirebaseEventType Supported events
- interface FirebaseEventOptions Event options
- type FirebaseSnapshot Data snapshot used as event handler argument
- class FirebaseConnector Connector class
Connector events
Child Added event
Event options:
type: 'ChildAdded'
ref: firebase.database.Reference | string
Event object:
Firebase's snapshot object
Example:
firebaseConnector.on({ type: 'ChildAdded', ref: 'mkKey' }, (snapshot) => {
console.log(`${snapshot.key} = ${snapshot.val()}`)
})
Child added in the realtime database. Learn more here.
Child Changed event
Event options:
type: 'ChildChanged'
ref: firebase.database.Reference | string
Event object:
Firebase's snapshot object
Example:
firebaseConnector.on({ type: 'ChildChanged', ref: 'mkKey' }, (snapshot) => {
console.log(`${snapshot.key} = ${snapshot.val()}`)
})
Child changed in the realtime database. Learn more here.
Child Moved event
Event options:
type: 'ChildMoved'
ref: firebase.database.Reference | string
Event object:
Firebase's snapshot object
Example:
firebaseConnector.on({ type: 'ChildMoved', ref: 'mkKey' }, (snapshot) => {
console.log(`${snapshot.key} = ${snapshot.val()}`)
})
Child moved in the realtime database. Learn more here.
Child Removed event
Event options:
type: 'ChildRemoved'
ref: firebase.database.Reference | string
Event object:
Firebase's snapshot object
Example:
firebaseConnector.on({ type: 'ChildRemoved', ref: 'mkKey' }, (snapshot) => {
console.log(`${snapshot.key} = ${snapshot.val()}`)
})
Child removed from the realtime database. Learn more here.
Value event
Event options:
type: 'Value'
ref: firebase.database.Reference | string
Event object:
Firebase's snapshot object
Example:
firebaseConnector.on({ type: 'Value', ref: 'mkKey' }, (snapshot) => {
console.log(`${snapshot.key} = ${snapshot.val()}`)
})
Value changed in the realtime database. Learn more here.
Connector actions
Database action
Definition:
() => firebase.database.Database
Usage:
const database = firebaseConnector.database()
Get the realtime database object.
Ref action
Definition:
(
path: string,
) => firebase.database.Reference
Usage:
firebaseConnector.ref('myKey').set('myValue')
Get a key reference in the realtime database.