{"id":19021617,"url":"https://github.com/codementorio/firebase-subscriber","last_synced_at":"2026-03-06T07:32:21.771Z","repository":{"id":4040025,"uuid":"51761602","full_name":"CodementorIO/firebase-subscriber","owner":"CodementorIO","description":"Firebase wrapper handling auth, re-auth, de-registering, and more.","archived":false,"fork":false,"pushed_at":"2023-02-02T18:05:27.000Z","size":2490,"stargazers_count":0,"open_issues_count":9,"forks_count":4,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-17T22:09:37.277Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CodementorIO.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-15T14:38:20.000Z","updated_at":"2021-12-15T04:38:53.000Z","dependencies_parsed_at":"2023-02-17T23:01:17.763Z","dependency_job_id":null,"html_url":"https://github.com/CodementorIO/firebase-subscriber","commit_stats":null,"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodementorIO%2Ffirebase-subscriber","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodementorIO%2Ffirebase-subscriber/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodementorIO%2Ffirebase-subscriber/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CodementorIO%2Ffirebase-subscriber/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CodementorIO","download_url":"https://codeload.github.com/CodementorIO/firebase-subscriber/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250395107,"owners_count":21423375,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T20:22:49.425Z","updated_at":"2026-03-06T07:32:21.729Z","avatar_url":"https://github.com/CodementorIO.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Firebase Subscriber\n[![Build Status](https://travis-ci.org/CodementorIO/firebase-subscriber.svg?branch=master)](https://travis-ci.org/CodementorIO/firebase-subscriber)\n[![npm](https://img.shields.io/npm/v/firebase-subscriber.svg)](https://www.npmjs.com/package/firebase-subscriber)\n\nFirebaseSubscriber is an abstract layer on top of [Firebase official SDK](https://firebase.google.com/docs/reference/js/).\nThe main purpose of FirebaseSubscriber is to:\n\n- Abstract logic token expiring/re-auth from application logic\n- Abstract event unsubscribing by an additional `Channel` layer\n\n## Installation\nThis lib does not include `firebase`, so you'll need to install it as well\n```\n$ yarn add firebase-subscriber firebase\n```\n\n## Usage\n\n```javascript\nconst FirebaseSubscriber = require('firebase-subscriber');\n\nconst getAuthToken = function() {\n  // request application api here to get fresh firebase auth token\n  // return a promise\n  // this function would be invoked whenever the firebase auth token is expired\n}\n\nconst subscribe = FirebaseSubscriber.subscriber({\n  appName: \"default\",\n  apiKey: \"AIza....\",                             // Auth / General Use\n  authDomain: \"YOUR_APP.firebaseapp.com\",         // Auth with popup/redirect\n  databaseURL: \"https://YOUR_APP.firebaseio.com\", // Realtime Database\n  storageBucket: \"YOUR_APP.appspot.com\",          // Storage\n  messagingSenderId: \"123456789\"                  // Cloud Messaging\n}, {\n  getAuthToken\n});\n\nsubscribe('/my-test-path').then((channel) =\u003e {\n  channel.on('child_added', function(val) {\n    // `val` here is the result of snapshot.val()\n    console.log('on child added', val);\n  })\n  channel.on('value', function(val) {\n    console.log('on value', val);\n  })\n\n  channel.off(); //=\u003e unsubscribe ALL event handlers bound on the channel\n});\n\n```\n\n## API\n\n### `.subscriber()`\n\nThe `.subscriber()` method takes two arguments, `firebaseConfig` and `options` for `Connection`, and returns a `Promise` for subscribing certain path of a database.\n\nPlease refer to the [Connection](#connection) section for the details of `options`.\n\n### Channel\n\nA `Channel` instance is returned by `subscribe` function.\n\n#### `channel.on(eventName, handler)`:\n\nWrap [Firebase.on()](https://www.firebase.com/docs/web/api/query/on.html),\ninvoke handler with `snapshot.val()`\n\n#### `channel.off()`:\n\nUnregister *ALL* event handlers on the channel\n\n#### `channel.onDisconnect(callback)`:\n\nInvoke callback with disconnected ref, for example:\n\n```javascript\nchannel.onDisconnect(function(presenceRef) {\n  presenceRef.set('offline')\n})\n```\n\n#### Setter Methods\n\n`Channel` instances are equipped with some setter methods simply delegate to its underlying firebase `ref`:\n\n- `channel.set()`\n- `channel.push()`\n- `channel.remove()`\n- `channel.update()`\n\n### Connection\n\n`Connection` is a configurable factory, which\n\n  - takes two arguments: `firebaseConfig` and `options`\n  - returns singleton connection, which would auto re-auth when expired\n\n#### `options`:\n\n| Option | Description |\n| --- | --- |\n| `getAuthToken` | A function which fetches firebase auth token from your application server and returns a promise |\n| `needAuth` | A flag to determine if user need to auth or not, default: `true` |\n| `isAnonymous` | A flag to determine if auth anonymously, default: `false` |\n\n#### Usage\n\n```javascript\nimport { Connection } from 'firebase-subscriber';\n\nconst getConnection = Connection(firebaseConfig, { getAuthToken });\nconst connection1 = getConnection();\nconst connection2 = getConnection();\n\nexpect(connection1).to.equal(connection2);\n```\n\n##### Auth Anonymously\n\n```javascript\n// specify `isAnonymous: true` in the options to create an anonymous connection\n// returns singleton connection with auto re-auth as well\nconst getConnection = Connection(firebaseConfig, { isAnonymous: true });\nconst connection = getConnection()\n```\n\n## Testing\n\n`$ npm test`\n\n## LICENCE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodementorio%2Ffirebase-subscriber","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodementorio%2Ffirebase-subscriber","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodementorio%2Ffirebase-subscriber/lists"}