UNPKG

@norniras/service-agent

Version:

Service agent made for handling incoming data and commands.

46 lines 3.56 kB
{ "name": "@norniras/service-agent", "version": "0.2.6", "description": "Service agent made for handling incoming data and commands.", "main": "./lib/main.js", "types": "./lib/main.d.ts", "files": [ "lib/" ], "author": "Jevgeni Glazunov", "license": "MIT", "dependencies": { "node-fetch": "^2.6.1" }, "devDependencies": { "@types/node-fetch": "^2.5.8", "dotenv": "^14.3.2", "ts-node-dev": "^1.1.8", "typescript": "^4.2.3" }, "engines": { "node": ">=14", "pnpm": ">=6" }, "repository": { "type": "git", "url": "git+https://github.com/norniras/service-agent.git" }, "keywords": [ "RTW", "Real Time Web", "Service Agent", "Nornir AS", "Hive", "Synx" ], "bugs": { "url": "https://github.com/norniras/service-agent/issues" }, "homepage": "https://github.com/norniras/service-agent#readme", "scripts": { "build": "tsc", "dev": "ts-node-dev ./src/examples/basic.ts" }, "readme": "# Service Agent for NodeJS\n\nAn agent should be used by the service owners for handling (send/receive) the data and commands.\n\n## Installation\n\n### npm\n\n```\nnpm i @norniras/service-agent\n```\n\n### yarn\n\n```\nyarn add @norniras/service-agent\n```\n\n### pnpm\n\n```\npnpm add @norniras/service-agent\n```\n\n## How to\n\n### Initialize the agent\n\n```javascript\nimport { ServiceAgent } from '@norniras/service-agent';\n\nconst agent = new ServiceAgent({\n serviceUrl: 'https://demo.cioty.com/service', // service URL\n token: 'aToken', // token\n ghostId: '0', // ghost id\n restartStream: true // Optional, default is true.\n});\n```\n\n### Agent - listen\n\nTakes callback as an argument that will handle incoming data and commands.\nThis is where all logic should be defined.\n\n```javascript\nagent.listen(({ data, command }) => {\n if (typeof data !== 'undefined') {\n // DATA HANDLER\n console.log(data);\n }\n\n if (typeof command !== 'undefined') {\n // COMMAND HANDLER\n console.log(command);\n }\n});\n```\n\n### Agent - send data\n\nThe service owner can send data to any ghost of his service.\n\n```javascript\nagent.sendData({\n ghostId: 'id', // Ghost ID where you want to send data\n dataString: 'key=value&key1=value1' // data is sent as query string\n});\n```\n\n### Agent - send command\n\nThe service owner can send commands only to the service he is linking to. Command schema defined by the service owner and can be found on micropage. We send to turn on lamp id 5 with a red color to a lamp service. The service agent of lamp service should handle this command.\n\n```javascript\nagent.sendCommand({\n targetUrl: 'https://demo.cioty.com/lamp', // service URL\n commandString: 'action=on&id=5&color=red'\n});\n```\n\n### Agent - get query string from an object\n\nFrom the examples above you can see that both data and commands should be sent as a query string. The method will convert objects into a query string.\n\n```javascript\nconst command = {\n action: 'on',\n id: 5,\n color: 'red'\n};\nconst commandString = agent.getQueryStringFromObject(command);\n\n// commandString // action=on&id=5&color=red\n\nagent.sendCommand({\n targetUrl: 'https://demo.cioty.com/lamp', // service URL\n commandString\n});\n```\n\n## For developers\n\n1. Clone repo on GitHub\n2. Inside **src/config** folder create **_.env_** and copy from **_example.env_**\n3. Fill up **_.env_** file\n4. Run **_pnpm dev_** to start an agent\n5. Using **CURLs** u can send some data to your agent and see how does it work" }