0.1.1 • Published 2 years ago

node-iot-lib v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

node-iot-lib

Node.js library for IoT projects

Usage

Backend Server

const PORT = 5000;

const { createClient } = require("redis");
const redisClient = createClient();
redisClient.on("connect", () => console.log("redis client connect"));
redisClient.connect();

const { Server } = require("socket.io");
const socketServer = new Server({ cors: { origin: true } });

const { IoTServer } = require("node-iot-lib");
const ioTServer = new IoTServer(redisClient, socketServer);

const http = require("http");
const app = require("./app");
const httpServer = http.createServer(app);

ioTServer.attach(httpServer);

httpServer.listen(PORT, () => {
  console.log("server is listening on port", PORT);
});

// [...]

Producer Server

const { createClient } = require("redis");
const redisClient = createClient();
redisClient.on("connect", () => console.log("redis client connect"));
redisClient.connect();

const { IoTProducer } = require("node-iot-lib");
const producer = new IoTProducer(redisClient);

// [...]

Then

producer.publish("hello", "world"));
// or
producer.publish("hello", JSON.stringify({ message: "world" }));

Consumer Server

const { createClient } = require("redis");
const redisClient = createClient();
redisClient.on("connect", () => console.log("redis client connect"));
redisClient.connect();

const { IoTConsumer } = require("node-iot-lib");
const consumer = new IoTConsumer(redisClient);

// [...]

How it works

Client Side

...

Server Side

Set of subscribed channels:

["channelA", "channelB"]

Set of clients in map of subscribed channels:

{
  "channelA": ["clientA", "clientB"],
  "channelB": ["clientA", "clientB"]
}

When a client subscribe to a channel

  • Add channel to Set of subscribed channels.
  • Add client to Set of clients in map of subscribed channels.