1.0.10 • Published 3 years ago
firesocket-server v1.0.10
Firesocket
Firesocket is a websocket connection library using firestore and cloud function.
Quick start
Allow to read
/documents/firesockets/{event}
like this:rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /firesocket/{event} { allow read: if "*" in resource.data.to || request.auth.uid in resource.data.to; allow write: if false; } } }
Create an index for firesocket collection as:
Key Val Collection id firesocket Fields to
arrays,at
ascendingScope collection Import firesocket-server to your cloud functions like this:
const functions = require("firebase-functions"); const { initializeApp } = require("firebase-admin/app"); const { Firesocket } = require("firesocket-server"); const app = initializeApp(); // intialize firesocket const firesocket = new Firesocket(app); // register firesocket handler exports.firesocketHandler = functions.https.onCall(firesocket.handler); // define functions firesocket.on("broadcast-message", (data, uid) => { firesocket.emit("view-message", { message: data.message, from: uid, }); }); firesocket.on("broadcast-message", (data, uid) => { firesocket.emit( "view-message", { message: data.message, from: null, }, [uid] ); });
Now, event listeners for
broadcast-message
andecho-message
are activated.
firesocket options
const options = {
collectionName: "firesocket" // name of collection used firesocket. if you change it, you also have to change collection name in rules and an index.
documentRetentionTime: 0 // time of document retention(ms). if it's 0, all document will be cleared instantly. if it's -1, document will not be cleared.
}
// intialize firesocket
const firesocket = new Firesocket(app, options);
LISENCE
MIT