1 | # API
|
2 |
|
3 | The `evejs` library contains the following:
|
4 |
|
5 | - [`eve.Agent`](#agent)
|
6 | - `eve.system` a default, global instance of a service manager,
|
7 | loaded with a `LocalTransport`.
|
8 | - [`eve.ServiceManager`](#servicemanager) construct a service manager.
|
9 | - [`eve.TransportManager`](#transportmanager) construct a service manager.
|
10 | - `eve.module.BabbleModule`
|
11 | - `eve.module.PatternModule`
|
12 | - `eve.module.RequestModule`
|
13 | - [`eve.transport.Transport`](#transport) (abstract prototype)
|
14 | - `eve.transport.AMQPTransport` using the [AMPQ](http://www.amqp.org/) protocol,
|
15 | for example via [RabbitMQ](https://www.rabbitmq.com/) servers.
|
16 | - `eve.transport.DistribusTransport` using [distribus](https://github.com/enmasseio/distribus).
|
17 | - `eve.transport.HTTPTransport` for messaging over http,
|
18 | - `eve.transport.LocalTransport` using a local, in process transport.
|
19 | - `eve.transport.PubNubTransport` using [PubNub](http://www.pubnub.com/).
|
20 | - `eve.transport.WebSocketTransport` using web sockets.
|
21 | - [`eve.transport.connection.Connection`](#connection) (abstract prototype)
|
22 | - `eve.transport.connection.AMQPConnection`
|
23 | - `eve.transport.connection.DistribusConnection`
|
24 | - `eve.transport.connection.HTTPConnection`
|
25 | - `eve.transport.connection.LocalConnection`
|
26 | - `eve.transport.connection.PubNubConnection`
|
27 | - `eve.transport.connection.WebSocketConnection`
|
28 | - `eve.util` containing some utility functions.
|
29 |
|
30 |
|
31 | ## Agent
|
32 |
|
33 | Constructor:
|
34 |
|
35 | ```js
|
36 | var agent = new eve.Agent([id: string]);
|
37 | ```
|
38 |
|
39 | Properties:
|
40 |
|
41 | - `Agent.ready : Promise`
|
42 | A promise which resolves when all connections of the agent are ready.
|
43 |
|
44 | Methods:
|
45 |
|
46 | - `Agent.send(to: string, message: string)`
|
47 | Send a message to an other agent. Parameter `to` is either:
|
48 |
|
49 | - A string "agentId", the id of the recipient. Will be send
|
50 | via the default transport or when there is no default
|
51 | transport via the first connected transport.
|
52 | - A string "protocol://networkId/agentId". This is a sharable
|
53 | identifier for an agent.
|
54 |
|
55 | - `Agent.extend(module: string | Array.<string> [, options: Object]): Agent`
|
56 | Extend an agent with modules (mixins). Available modules:
|
57 | - `'pattern'`
|
58 | Add support for pattern listening to an object. The agent will be extended
|
59 | with functions `listen` and `unlisten`. Cannot be used in conjunction with
|
60 | module `'babble'`.
|
61 |
|
62 | - `'request'`
|
63 | Add support for sending requests and immediately retrieving a reply. The
|
64 | agent will be extended with a function `request`.
|
65 |
|
66 | - `'babble'`
|
67 | Babblify an agent. The babblified agent will be extended with functions
|
68 | `ask`, `tell`, and `listen`. Cannot be used in conjunction with
|
69 | module `'pattern'`.
|
70 |
|
71 | The function `extend` returns the agent itself, which allows chaining multiple
|
72 | extenstions.
|
73 |
|
74 | - `Agent.loadModule(module: string | Array.<string> [, options: Object]): Module`
|
75 | Load a module for an agent. This is the same as `Agent.extend`, except that
|
76 | the functions offered by the module are not attached to the Agent itself, but
|
77 | returned as object. This allows storing the new functions in a separate
|
78 | namespace, preventing conflicts between modules or the agent itself.
|
79 |
|
80 | - `Agent.receive(from: string, message: string)`
|
81 | Receive a message from an agent. The method should be overloaded with an
|
82 | implementation doing something with incoming messages.
|
83 |
|
84 | - `Agent.connect(transport: Transport | Transport[] | string | string[] [, id: string]) : Promise<Agent, Error>`
|
85 | Connect the agent to a transport instance or the id of a transport loaded
|
86 | in `eve.system`. Parameter `transport` can be an Array to connect to multiple
|
87 | transports at once. Eve comes with multiple message transport implementations
|
88 | (see [Transports](transports.md).
|
89 | By default, the agent connects to the transport with it's
|
90 | own id. It is possible to provide an alternative id instead by specifying
|
91 | this as second argument.
|
92 |
|
93 | - `Agent.disconnect([transport: Transport | Transport[] | string | string[]])`
|
94 | Disconnect the agent from a transport or multiple transports. When parameter
|
95 | `transport` is not provided, the agent will be disconnected from all
|
96 | transports.
|
97 |
|
98 |
|
99 | ## ServiceManager
|
100 |
|
101 | A ServiceManager is an object to manage services for the agents. Currently,
|
102 | the only available service is transports. Typically, a service manager is
|
103 | provided to an agent on construction, allowing the agent to connect to relevant
|
104 | services.
|
105 |
|
106 | A ServiceManager is created as:
|
107 |
|
108 | ```js
|
109 | var services = new eve.ServiceManager();
|
110 | var services = new eve.ServiceManager(config: Object);
|
111 | ```
|
112 |
|
113 | Where `config` is an optional JSON object structured as:
|
114 |
|
115 | ```js
|
116 | {
|
117 | "transports": [
|
118 | {
|
119 | "type": STRING,
|
120 | ... transport dependent params
|
121 | },
|
122 | ...
|
123 | ],
|
124 | "timer": {
|
125 | "rate": NUMBER | "discrete"
|
126 | },
|
127 | "random": {
|
128 | "deterministic": BOOLEAN
|
129 | }
|
130 | }
|
131 | ```
|
132 |
|
133 | Properties:
|
134 |
|
135 | - `transports: TransportManager` see [TransportManager](#transportmanager)
|
136 | - `timer: hypertimer` see [Timer](configuration.md#timer)
|
137 | - `random: function` see [Random](configuration.md#random)
|
138 |
|
139 | Methods:
|
140 |
|
141 | - `clear()`
|
142 | Close all configured services and remove them from the manager.
|
143 | - `init(config: Object)`
|
144 | Initialize the service manager with services loaded from a configuration
|
145 | object. All current services are unloaded and removed.
|
146 |
|
147 |
|
148 |
|
149 | ## TransportManager
|
150 |
|
151 | A TransportManager manages transports for reuse by multiple agents.
|
152 |
|
153 | A TransportManager is created as:
|
154 |
|
155 | ```js
|
156 | var services = new eve.TransportManager();
|
157 | var services = new eve.TransportManager(config: Array);
|
158 | ```
|
159 |
|
160 | Where `config` is an optional JSON array structured as:
|
161 |
|
162 | ```js
|
163 | [
|
164 | {
|
165 | "type": STRING,
|
166 | ... transport dependent params
|
167 | },
|
168 | ...
|
169 | ]
|
170 | ```
|
171 |
|
172 | Available types: `"amqp"`, `"distribus"`, `"local"`, `"pubnub"` and `"http"`.
|
173 | The configuration for all available transports is described in detail on the
|
174 | page [Transports](transports.md).
|
175 |
|
176 | Methods:
|
177 |
|
178 | - `add(transport: Transport) : Transport`
|
179 | Add a loaded transport to the manager. Returns the transport itself.
|
180 | - `clear()`
|
181 | Close all configured transports and remove them from the manager.
|
182 | - `get(id: string) : Transport`
|
183 | Get a single transport by it's id.
|
184 | Throws an error when no matching transport is found.
|
185 | - `getAll() : Transport[]`
|
186 | Get all transports.
|
187 | - `getByType([type: string]) : Transport[]`
|
188 | Get transports. When optional parameter `type` is provided, the transports
|
189 | are filtered by this type. When there are no transports found, an empty
|
190 | array is returned.
|
191 | Available types are: 'amqp', 'distribus', 'local', 'pubnub'.
|
192 | - `load(config: Object) : Transport`
|
193 | Load a transport based on JSON configuration. Returns the loaded transport
|
194 | - `registerType(constructor: Function)`
|
195 | Register a new type of transport. This transport can then be loaded via
|
196 | configuration. When called, the constructor must generate a transport which
|
197 | is an instance of `Transport`.
|
198 |
|
199 |
|
200 | ## Transport
|
201 |
|
202 | The library provides multiple `Transport` implementations: `LocalTransport`,
|
203 | `PubNubTransport`, `AMQPTransport`, and `DistribusTransport`. It is quite
|
204 | easy to implement a support for other messaging services.
|
205 |
|
206 | `Transport` has the following API:
|
207 |
|
208 | Constructor:
|
209 |
|
210 | ```js
|
211 | var transport = new Transport([config: Object]);
|
212 | ```
|
213 |
|
214 | Methods:
|
215 |
|
216 | - `Transport.connect(id: string, receive: Function) : Connection`
|
217 | Connect an agent with given `id`. When a message for the agent comes in,
|
218 | the callback function `receive` is invoked as `receive(from: string,
|
219 | message: string)`. The method returns a [`Connection`](#connection), which
|
220 | can be used to send messages.
|
221 |
|
222 | - `Transport.close() : Connection`
|
223 | Close a transport. For example in case of an `HTTPTransport`, the server
|
224 | will be closed.
|
225 |
|
226 |
|
227 | ## Connection
|
228 |
|
229 | When a connection is opened by a [`Transport`](#transport), a `Connection` is
|
230 | returned.
|
231 |
|
232 | Constructor:
|
233 |
|
234 | ```js
|
235 | var connection = new Connection(transport: Transport, id: string, receive: function);
|
236 | ```
|
237 |
|
238 | Properties:
|
239 |
|
240 | - `Transport.ready: Promise`
|
241 | The ready state of the transport. When ready, the properties Promise resolves.
|
242 |
|
243 | Methods:
|
244 |
|
245 | - `Transport.close()`
|
246 | Close the connection.
|
247 | - `Transport.send(to: string, message: string)`
|
248 | Send a message via the transport.
|