@aeontek/eventservice
v1.0.16
Published
A service for creating, sending, and handling an event pattern implementation either locally or with WebSockets
Downloads
3
Readme
Classes
Interfaces
Message
Kind: global interface
Properties
| Name | Type | Description | | --- | --- | --- | | id | string | The unique identifier for this message. This may be valuable for distinguising between iterations of a single event that it called multiple times. | | destination | string | undefined | Optional. The intended recipient service of this message. If the service is not registered with the Server, the message will be ignored. If this argument is omitted, it is assumed that the Event is meant to be handled locally. | | origin | string | The service that the Event originated from. | | eventId | string | An identifier for the Event. While the identifier must be unique for Events on a service, different services may share Event identifiers. It is also possible for a single event to be raised multiple times. | | payload | T | Optional. If provided, this generic (T) object will be passed to events at the destination service as the data for event handlers. |
Client
Client Creates a WebSocket Client for handling events across different applications. Requires a running Server.
Kind: global class
client.run(port, serviceName, ip)
Initializes the EventService Client.
Kind: instance method of Client
| Param | Type | Description | | --- | --- | --- | | port | number | The port number to connect to. | | serviceName | string | The name of the service. Must be unique, and must be registered with the server. | | ip | string | If provided, the IP address of the WebSocket Server. If this is left null, then the Client will assume the WebSocket Server is hosted at localhost. |
client.send(message)
Sends a Message to the Server, which will route the message to its destination.
Kind: instance method of Client
| Param | Type | Description | | --- | --- | --- | | message | Message | The message object which contains the parameters needed to handle the message, as well as the message itself. |
client.stop()
Stops the client
Kind: instance method of Client
Event
Event The Event class contains methods for creating and managing event handlers.
Kind: global class
- Event
- .addListener(callback, id) ⇒ string
- .removeListener(id)
- .raise(data, destination)
- .listListeners() ⇒ Array.<string>
event.addListener(callback, id) ⇒ string
Adds a listener to the Event. If the Event is raised, then any registered listeners will be called.
Kind: instance method of Event
Returns: string - Returns the unique identifier of the event handler, which
can be used to remove the listener.
Throws:
- Invalid Identifier error if id is not unique
| Param | Type | Description | | --- | --- | --- | | callback | function | The function to call when the Event is raised. | | id | string | If provided, will act as the identifier for this event handler. The id must be uniquem or an error will be thrown. |
event.removeListener(id)
Removes a litener using its unique identifier
Kind: instance method of Event
| Param | Type | Description | | --- | --- | --- | | id | string | The unique identifier of the event to be removed. |
event.raise(data, destination)
Raises the Event. When an Event is raised, all of its event handlers are called.
Kind: instance method of Event
| Param | Type | Description | | --- | --- | --- | | data | any | The data that is sent along to an event handler as an argument | | destination | string | If a destination is specified and the service has been initalized as either a server or a client, then this will direct the server to forward this event to the correct service, given that the service is registered with the server. |
event.listListeners() ⇒ Array.<string>
Lists the unique identifiers of the current listeners.
Kind: instance method of Event
EventService
Service that handles the creation and management of events.
Kind: global class
EventService.Event(id) ⇒ Event
Event factory.
If called with the id of an existing Event, will return that Event. Otherwise, will create and return a new Event. If the service has been initalized as either a client or server, this will also add an event handler for pushing event data to the appropriate WebSockets.
Kind: static method of EventService
| Param | Type | Description | | --- | --- | --- | | id | string | The unique identifier for the event. |
Server
Server Creates a WebSocket Server for the managing of events across multiple applications. Allows connections only from Clients that have been registered using server.registerService(serviceName).
Kind: global class
server.run(port)
Runs the WebSocket Server, which listens for, processes, and emits the events that makes all the different services communicate. In order for the EventService to function between applications, exactly one must be functioning as a server.
Kind: instance method of Server
| Param | Type | Description | | --- | --- | --- | | port | number | The port to host the Server on. |
server.registerService(serviceName)
Adds the given service name to the list of registered services.
Kind: instance method of Server
Throws:
- "Invalid Identifier" if the entry already exists.
| Param | Type | Description | | --- | --- | --- | | serviceName | string | The name of the service being added. |
server.send(message)
Sends a Message to the appropriate service, based on the Message.destination.
Kind: instance method of Server
| Param | Type | Description | | --- | --- | --- | | message | Message | The message object which contains the parameters needed to handle the message, as well as the message itself. |
server.stop()
Stops the server
Kind: instance method of Server