Event Service

Event Service provides methods to interact with the event server which implements the publish/subscribe messaging paradigm where one component publishes an event and all clients that have subscribed receive the event.

Pre-requisite

  1. The Location Service, Event Service and Gateway Server needs to be running in the network
  2. The necessary configuration, environment variables or system properties should be defined to point to the correct host and port number(s) for the Location Service nodes.

Creation of Event Service

Examples to create a client for Event Service

Typescript
sourceconst eventService: EventService = await EventService()

Usages of Event Service

Type definitions for all Event Service methods can be found here.

Publishing an Event

Event Service allows you to publish an Event to the event server.

Type definitions for publish method can be found here.

Typescript
sourceconst sourcePrefix = new Prefix('IRIS', 'component')
const eventName = new EventName('move-event')
const positionParameter = intKey('positions', Units.centimeter).set([10, 20, 30])
const paramSet: Parameter<IntKey>[] = [positionParameter]

const event = ObserveEvent.make(sourcePrefix, eventName, paramSet)

const done: Done = await eventService.publish(event)

Get Events

This method is used to get events for set of EventKeys. This is different from subscribing to event. Use this method to get occurred events. If you want to consume live events, use subscription methods.

Type definitions for get method can be found here.

Example for getting events:

Typescript
sourceconst sourcePrefix = new Prefix('IRIS', 'component')

const eventKey1 = new EventKey(sourcePrefix, new EventName('eventKey1'))
const eventKey2 = new EventKey(sourcePrefix, new EventName('eventKey2'))

const eventKeys = new Set([eventKey1, eventKey2])
const events: Event[] = await eventService.get(eventKeys)

Subscribe to the Events

Subscribing to Event can be done via two ways. User can subscribe to -

  1. Multiple EventKeys.
  2. All the EventKeys of specific subsystem and pattern.

When you subscribe for the given EventKeys, events are received at every frequency. The methods take callback function which gets triggered whenever the events are received.

Type definitions of both methods can be found by below links :

  1. subscribe.
  2. pSubscribe.

Example for subscribing events with given EventKeys:

Typescript
sourceconst sourcePrefix = new Prefix('IRIS', 'component')

const eventKey1 = new EventKey(sourcePrefix, new EventName('eventKey1'))
const eventKey2 = new EventKey(sourcePrefix, new EventName('eventKey2'))

const onEventCallback = (event: Event) => {
  console.log(event)
  // make use of ${event} inside this callback function
}
const eventKeys = new Set([eventKey1, eventKey2])
//optional
const onErrorCallback = (error: ServiceError) => {
  // do something when error occurs
  // for ex : close connection / cleanup resources
  console.log(error)
}
//optional
const onCloseCallback = () => {
  // do something when connection is closed
  // for ex : reset client-side state
}
const subscription: Subscription = eventService.subscribe(eventKeys, 10)(
  onEventCallback,
  onErrorCallback,
  onCloseCallback
)

//To cancel the subscription
subscription.cancel()

Example for subscribing events with given subsystem:

Typescript
sourceconst onEventCallback = (event: Event) => {
  // make use of ${event} inside this callback function
  console.log(event)
}
//optional
const onErrorCallback = (error: ServiceError) => {
  // do something when error occurs
  // for ex : close connection / cleanup resources
  console.log(error)
}
//optional
const onCloseCallback = () => {
  // do something when connection is closed
  // for ex : reset client-side state
}
// subscribe to all ESW subsystem's event
const subscription: Subscription = eventService.pSubscribe('ESW', 10, '.*')(
  onEventCallback,
  onErrorCallback,
  onCloseCallback
)

// subscribe to specific events having hcd in the event name
const specificSubscription: Subscription = eventService.pSubscribe('ESW', 10, '(hcd)')(onEventCallback)

//To cancel the subscription
subscription.cancel()