subote
v0.4.2
Published
unidirectional data flow RPC framework
Downloads
8
Readme
subote
unidirectional data flow RPC framework
Installation
npm install subote --save
API
Router : Duplex
Router manage messages and switching context. This class should not used directly in ordinary. You can use Server/Client classes instead of this class.
constructor(socket: Socket, [namespace: string])
Instance attributes
socket: Socket
namespace: string
shared: object
currentContext: Context
currentContextName: string
Instance methods
dispatch a message
register(address: string, subscription: function): void
register(subscription: function): void
register(subscription: { delegate: function }): void
- register a given callback function or delegator
unregister(address: string, subscription: function): void
unregister(subscription: function): void
unregister(subscription: { delegate: function }): void
- unregister a given callback function or delegator
dispatch(address: string, data: any): void
- send a message to subscriptions
receive a message
delegate(address: string, data: any): void
- called automatically by message publisher
for sending a message (via network)
send(address: string, data: any, [options: object])
- server side: send a message to all clients via network (aka broadcast)
- client side: send a message to the server router (is not a client binded in the server) via network
- options are used for socket flags
volatile: boolean
json: boolean
for managing contexts
registerContext(name: string, context: Context): void
switchContext(name: string): Promise<void>
Address
address: string
must start with / . Address is used for dispatching a message, or name of action that is received it.
Actions
Action is instance method that starts with / . It is called automatically by messages from other dispatcher.
Server : Router
Router for server side.
constructor(socket: Socket, [namespace: string])
Instance attributes
clients: Socket[]
socket.send(address: string, data: any): void
- send a message to a client binded via network
Client : Router
Router for client side.
constructor(socket: Socket, [namespace: string])
Context : Delegator
Context represents separate actions.
constructor()
Instance attributes
router: Router
shared: object
Instance methods
for sending a message
send(address: string, data: any)
- shortcut of
router.send
- shortcut of
dispatch(address: string, data: any): void
- shortcut of
router.dispatch
- shortcut of
for receiving a message
delegate(address: string, data: any)
- called automatically by router
for managing contexts
resume(): Promise<void>
suspend(): Promise<void>
Actions
Action can be defined in each context. A context action is called after a router action.
Messaging Protocol
+--------+ +-------------+
| source | <-- register --- | destination |
| | --- dispatch --> | |
+--------+ +-------------+
interface source {
register(address: string, subscription: function): void;
register(subscription: function): void;
register({ delegate: function }): void;
};
interface destination {
delegate(address: string, data: any): void;
};
Source needs to have a register
method for a destination subscribes messages.
When source dispatch a message, they needs to call destination's delegate
method.
In subote architecture, address: string
must start with / . When subote.Router instance is received a message, it automatically calls an action method with data.
let source = new subote.Dispatcher();
let router = new subote.Router(socket);
router["/message/view"] = (data) => {
console.log(`received: ${data}`);
};
source.register(router);
source.dispatch("/message/view", "hello!");
// -> call router.delegate("/message/view", "hello!")
// => "received: hello!"
License
MIT