@hidetak/binding-wotfirestore
TypeScript icon, indicating that this package has built-in type declarations

0.0.4 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i @hidetak/binding-wotfirestore

Weekly Downloads

4

Version

0.0.4

License

EPL-2.0

Unpacked Size

120 kB

Total Files

30

Last publish

Collaborators

  • hidetak