rf-api-websocket
rf-api implementation for RPC over websockets using JSON messages with optional ACL.
ws server
- Uses express server instance
- Websocket communication service
- Allows safe RPC
- Uses JSON messages
ws methods
addHandler
Add a simple callback handler. Errors can be signalled via exceptions Any previous handler with the same func name will be replaced.
services
funcName
The name of the handler. In order for the handler to be called, this needs to be used in the func attribute of the received websocket message.callback
The handler function(msg, responseCallback(msg))acl
Optional ACD configuraton
addPromiseHandler
Add a promise callback handler that either resolves to null (no response) or to response data and signals exceptions via rejection (logged, no response. Any previous handler with the same func name will be replaced.
services
funcName
The name of the handler. In order for the handler to be called, this needs to be used in the func attribute of the received websocket message.callback
The handler function(msg, responseCallback(msg))acl
Optional ACD configuraton
sendObj
services
TODO: integrate callbackID maybe one raw send method, and one preconfigured (default to use)
broadcast
Send the given object to ALL the currently connected websockets NOTE: This sends the object as-is.
services
Getting started
start the package
When the module is started, the websocket server and handler is automatically registered against the HTTP server. You dont need to start the server manually!
// prepare backendvar config = ; // configvar http = start // webserver pathsWebserver: configpathswebserver port: configport;var API = startapp: httpapp; // prepare apivar mongooseMulti = ; // databasesvar db = mongooseMultistartconfigdburls configpathsschemas; dbglobalmongooseConnection;
Use Websocket requests
Websocket messages have the form: {func: "", data: {...}, token: ""} Any other attributes are copied to the response
Register a handler like this:
API // Empty ACL => no auth required
See the rf-acl
package for documentation about the ACL syntax
If you are using Promises, use this syntax
API // Empty ACL => no auth required
Development
Install the dev tools with
npm install
Then you can runs some test cases and eslint with:
npm test
Generate Docs:
npm run-script doc
To Do
- get the everything running
Legal Issues
- License: MIT
- Author: Rapidfacture GmbH