Create a Custom Eventsource
About Eventsources
An eventsource is any entity or technology responsible for capturing events or notifications when specific events or conditions occur. These events are consumed by event handlers or processors for real-time or near-real-time responses. Eventsources can include Sync and Async event sources like Message brokers, Webhooks etc.The settings for each datasource lies in src/eventsources directory.
Any Eventsource
Steps to create custom eventsource :
let's use Express as an example of eventsource :
let's use cron as an example of eventsource :
- Inside the
eventsources
directory, create aYAML
file with a specific name. In this YAML file, ensure you specify atype
field, and there must be a correspondingTypeScript
file in thetypes
directory that shares the same name as thetype
you defined.
├── src
├── datasources
│
├── events
| |
│ └── helloworld.yaml
|
├── eventsources
│ ├── types
│ | └── custom_eventsource.ts
| |
│ └── custom_eventsource.yaml
|
└── functions
|
└── helloworld.yaml
In your TypeScript file, use an import statement to bring in
GSEventSource
from the@godspeedsystems/core
package. Then, create a class that inherits fromGSEventSource
.Afterward, you can access the methods provided by
GSEventSource
. Initialize your client by calling theinitClient()
function.Once your client is initialized, you can execute its subscription using the
subscribeToEvent
function.
Datasource as eventsource
There are special cases when datasource can also act as an eventsource. For eg: Kafka can be used both datasource as well as eventsource. When we are publishing message to kafka, it can work as a datasouce .But when we are listening to events on kafka, then it is event source also, then the same client can serve as both.
Steps to create custom datasource as eventsource :
let's use kafka as an example of an eventsource :
- To create datasource follow How to create custom datasource
.
├── src
├── datasources
│ ├── types
│ | └── custom_datasource.ts
| |
│ └── custom_datasource.yaml
│
├── events
| |
│ └── helloworld.yaml
|
├── eventsources
│ ├── types
│ | └── custom_eventsource.ts
| |
│ └── custom_eventsource.yaml
|
└── functions
|
└── helloworld.yaml
To create eventsource, Inside the
eventsources
directory, create aYAML
file with a specific name. In this YAML file, ensure you specify atype
field, and there must be a correspondingTypeScript
file in thetypes
directory that shares the same name as thetype
you defined.In your TypeScript file, use an import statement to bring in
GSEventSource
from the@godspeedsystems/core
package. Then, create a class that inherits fromGSEventSource
.Your client is initialized already in datasource so you can execute its subscription using the
subscribeToEvent
function.