--- title: EventClient weight: 10000 --- ## Classes
EventClient

EventClient abstracts the connection to the bot websocket for events

## Typedefs
Options : Object

Options to pass to the EventClient constructor

SocketMessage : Object

SocketMessage received for every event and passed to the new (eventObj) => { ... } handlers

## EventClient EventClient abstracts the connection to the bot websocket for events **Kind**: global class * [EventClient](#EventClient) * [new EventClient(opts)](#new_EventClient_new) * [.apiBase()](#EventClient+apiBase) ⇒ string * [.paramOptionFallback(key, [fallback])](#EventClient+paramOptionFallback) ⇒ \* * [.renderTemplate(template)](#EventClient+renderTemplate) ⇒ Promise * [.replayEvent(eventId)](#EventClient+replayEvent) ⇒ Promise ### new EventClient(opts) Creates, initializes and connects the EventClient | Param | Type | Description | | --- | --- | --- | | opts | [Options](#Options) | Options for the EventClient | ### eventClient.apiBase() ⇒ string Returns the API base URL without trailing slash **Kind**: instance method of [EventClient](#EventClient) **Returns**: string - API base URL ### eventClient.paramOptionFallback(key, [fallback]) ⇒ \* Resolves the given key through url hash parameters with fallback to constructor options **Kind**: instance method of [EventClient](#EventClient) **Returns**: \* - Value of the key or `null` | Param | Type | Default | Description | | --- | --- | --- | --- | | key | string | | The key to resolve | | [fallback] | \* | | Fallback to return if neither params nor options contained that key | ### eventClient.renderTemplate(template) ⇒ Promise Renders a given template using the bots msgformat API (supports all templating you can use in bot messages). To use this function the token passed through the constructor or the URL hash must have the `msgformat` permission in addition to the `overlays` permission. **Kind**: instance method of [EventClient](#EventClient) **Returns**: Promise - Promise resolving to the rendered output of the template | Param | Type | Description | | --- | --- | --- | | template | string | The template to render | ### eventClient.replayEvent(eventId) ⇒ Promise Triggers a replay of the given event to all overlays currently listening for events. This event will have the `is_live` flag set to `false`. **Kind**: instance method of [EventClient](#EventClient) **Returns**: Promise - Promise of the fetch request | Param | Type | Description | | --- | --- | --- | | eventId | Number | The ID of the event received through the SocketMessage object | ## Options : Object Options to pass to the EventClient constructor **Kind**: global typedef **Properties** | Name | Type | Default | Description | | --- | --- | --- | --- | | [channel] | String | | Filter for specific channel events (format: `#channel`) | | [handlers] | Object | {} | Map event types to callback functions `(eventObj) => { ... }` (new) or `(event, fields, time, live) => {...}` (old) | | [maxReplayAge] | Number | -1 | Number of hours to replay the events for (-1 = infinite) | | [replay] | Boolean | false | Request a replay at connect (requires channel to be set to a channel name) | | [token] | String | | API access token to use to connect to the WebSocket (if not set, must be provided through URL hash) | ## SocketMessage : Object SocketMessage received for every event and passed to the new `(eventObj) => { ... }` handlers **Kind**: global typedef **Properties** | Name | Type | Description | | --- | --- | --- | | [event_id] | Number | UID of the event used to re-trigger an event | | [is_live] | Boolean | Whether the event was sent through a replay (false) or occurred live (true) | | [reason] | String | Reason of this message (one of `bulk-replay`, `live-event`, `single-replay`) | | [time] | String | RFC3339 timestamp of the event | | [type] | String | Event type (i.e. `raid`, `sub`, ...) | | [fields] | Object | string->any mapping of fields available for the event |