npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hidetak/binding-wotfirestore

v0.0.4

Published

Firestore binding for node-wot

Downloads

6

Readme

Firestore Binding of node-wot

Overview

Firestore Binding is an implementation of the W3C Web of Things Binding using Firestore, which can be used with Eclipse Thingweb node-wot to embody the Web of Things.

Protocol specifier

The protocol prefix handled by this binding is wotfirestore://.

Getting Started

In the following examples it is shown how to use the Firestore binding of node-wot.

You must have a Firebase account to use this binding.

Setting up Firebase

In order to use Firestore Binding, you need to set up Firebase. Perform the following setup from the Firestore console.

  1. Getting started with Authentication
  2. Enable email/password for Sign-in Method in Authentication
  3. Add user in Users (A)
  4. Create a database in Cloud Firestore
  5. Edit database rule of Firestore to require user authentication for access
    rules_version = '2';
    service cloud.firestore {
        match /databases/{database}/documents {
            match /{document=**} {
                allow read, write: if request.auth.uid != null;
            }
        }
    }
  6. Select Project Settings from the top left gear and confirm the Project ID (B) and Web Api Key (C).

The values defined in (A), (B) and (C) will be written in the configuration file shown in Confiuration chapter.

Advance preparation

To prepare for creating a nodejs app, execute npm install. After executing npm init, install the following modules.

  • npm install @node-wot/core
  • npm install @hidetak/binding-wotfirestore

Creating configuration file

These examples require a configuration file that contains Firestore connection information and other information. This configuration file should contain the following information((A), (B), and (C) correspond to the information written in the Setting up Firebase chapter):

{
    "hostName": "<Host Name defined by user>",
    "firebaseConfig": {
        "apiKey": "<API Key of Firebase (C)>",
        "projectId": "<Project Id of Firebase (B)>",
        "authDomain": "<Auth Domain of Firebase(Usually it will be <B>.firebaseapp.com>"
    },
    "user": {
        "email": "<email address registered in Firebase (A)>",
        "password": "<password corresponding to the email address above (A)>"
    }
}

The information needed to create the configuration file can be found at Firestore console.

Client Example

The client example tries to connect to MyCounter thing via Firestore and reads a property count. The ThingDescription is stored in Firebase which can be access by client as wotfirestore://sample-host/MyCounter. The ThingDescription registered by example-server.js.

node example-client.js

// example-client.js
const Servient = require("@node-wot/core").Servient
const WoTFirestoreClientFactory = require("@hidetak/binding-wotfirestore").WoTFirestoreClientFactory
const Helpers = require("@node-wot/core").Helpers
const WoTFirestoreCodec = require("@hidetak/binding-wotfirestore").WoTFirestoreCodec

const firestoreConfig = require("./firestore-config.json")

let servient = new Servient()
const clientFactory = new WoTFirestoreClientFactory(firestoreConfig)
servient.addClientFactory(clientFactory)

const codec = new WoTFirestoreCodec()
servient.addMediaType(codec)
  
let wotHelper = new Helpers(servient)
wotHelper.fetch("wotfirestore://sample-host/MyCounter").then(async (td) => {
    try {
        servient.start().then((WoT) => {
            WoT.consume(td).then((thing) => {
                // read a property "count" and print the value
                thing.readProperty("count").then((s) => {
                    console.log(s);
                })
            })
        })
    } catch (err) {
        console.error("Script error:", err)
    }
}).catch((err) => { console.error("Fetch error:", err) })

Server Example

The server example produces a thing that allows for setting a property count. The thing is reachable via Firestore.

node example-server.js

// example-server.js
const Servient = require("@node-wot/core").Servient
const WoTFirestoreServer = require("@hidetak/binding-wotfirestore").WoTFirestoreServer
const WoTFirestoreCodec = require("@hidetak/binding-wotfirestore").WoTFirestoreCodec

const firestoreConfig = require("./firestore-config.json")

// create server
const server = new WoTFirestoreServer(firestoreConfig)

// create Servient add Firebase binding
let servient = new Servient();
servient.addServer(server);

const codec = new WoTFirestoreCodec()
servient.addMediaType(codec)

servient.start().then((WoT) => {
    WoT.produce({
        "@context": "https://www.w3.org/2019/wot/td/v1",
        title: "MyCounter",
        properties: {
			count: {
                type: "integer",
                observable: true,
                readOnly: false        
            }
        }
    }).then((thing) => {
        console.log("Produced " + thing.getThingDescription().title);
        thing.writeProperty("count", 0)

        thing.expose().then(() => {
            console.info(thing.getThingDescription().title + " ready");
            console.info("TD : " + JSON.stringify(thing.getThingDescription()));
            thing.readProperty("count").then((c) => {
                console.log("count is " + c)
            })
        })
    })
})

Notes

Because communication with Firestore occurs, you may be charged for using Firebase. In order to avoid unexpected charges, please check your usage. You can check it at Firebase console.