Skip to content

Instantly share code, notes, and snippets.

@timrijckaert
Last active Oct 7, 2020
Embed
What would you like to do?
Fetch Firebase tokens for topic

Firebase Topic Counter

This little script will output the amount of tokens subscribed to a topic.
You will need to replace the variables with your personal ones in order for it to work.

We used this function in a Lambda which sends the results to a monitoring dashboard.

How to get the variables

Screenshots in the comments.

var sha1 = require('sha1');
var fetch = require('node-fetch');
//See README.md
var key = "key";
var projectId = "firebase project id;
//Check cookies from https://console.firebase.google.com
var SID = "sid";
var HSID = "hsid";
var SSID = "ssid";
var APISID = "apisid";
var SAPISID = "sapisid";
exports.handler = async(event) => {
const origin = "https://console.firebase.google.com";
const secretFromDevConsole = `https://gcmcontextualcampaign-pa.clients6.google.com/v1/4/projects/${projectId}/topics?prefix=/topics/&key=${key}`;
const generateAuthorizationHeader = () => {
const currentTime = new Date().getTime();
const sapisid = SAPISID;
const hashed = sha1(`${currentTime} ${sapisid} ${origin}`);
return `SAPISIDHASH ${currentTime}_${hashed}`;
};
const getTopicSubscriptionsCall = async() => {
const authorizationKey = generateAuthorizationHeader();
await fetch(secretFromDevConsole, {
method: "GET",
headers: {
"Authorization": authorizationKey,
"X-Origin": origin,
"cookie": `SID=${SID}; HSID=${HSID}; SSID=${SSID}; APISID=${APISID}; SAPISID=${SAPISID}`
},
})
.then(response => response.json())
.then(async json => {
const topics = json.topic;
topics.forEach(topic => console.log(topic.name + " " + topic.size));
return { statusCode: 200 };
});
};
await getTopicSubscriptionsCall();
};
{
"name": "check-firebase-breaking-topic-subscriptions",
"version": "1.0.0",
"description": "Check Firebase breaking topic subscription",
"main": "index.js",
"dependencies": {
"node-fetch": "^2.3.0",
"sha1": "^1.1.1"
},
"author": "Tim Rijckaert",
"license": "ISC"
}
@timrijckaert

This comment has been minimized.

Copy link
Owner Author

@timrijckaert timrijckaert commented May 16, 2019

Screenshot from 2019-05-17 00-12-14
Screenshot from 2019-05-17 00-13-50
Screenshot from 2019-05-17 00-14-27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment