JavaScript library for interacting with the Event Gateway.
Node:
npm install @serverless/event-gateway-sdk
Browser:
<script type="text/javascript" src="https://unpkg.com/@serverless/event-gateway-sdk@latest/dist/event-gateway-sdk.min.js"></script>
The EventGateway SDK will then be attached to window e.g. and you can access it via window.EventGatewaySDK
When using in your application logic, you'll usually interact with the invoke
and emit
APIs.
Use the invoke
command to synchronously invoke backend function by name. This is similar to an RPC call.
// Construct your client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
url: 'http://myeventgateway.io',
space: 'prod'
})
// Call your function
eventGateway.invoke({
functionId: 'users.getUsers',
data: { 'limit': 100 }
}).then((resp) => resp.json())
.then((users) => console.log(users))
Use the emit
command to emit a named event and payload to your Event Gateway. The event will be received by any function that is subscribed to your event.
// Construct your client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
url: 'http://myeventgateway.io',
space: 'prod'
})
// Emit your event
eventGateway.emit({
event: 'user.completedTutorial'
data: { 'userId': 1234 }
})
Parameters
Object:
url
-string
- required, Events API URLconfigurationUrl
-string
- Configuration API URL. By default, it's the same asurl
but with4001
portspace
-string
- Space, default:default
apikey
-string
- API key for hosted Event Gateway.fetchClient
-object
-fetch
client
const SDK = require('@serverless/event-gateway-sdk');
const eventGateway = new SDK({
url: 'http://localhost',
space: 'mycompany-prod',
apikey: '1234abcd'
})
Returns
Promise object resolving to array of function objects
eventGateway.listFunctions()
Parameters
Object:
functionId
-string
- function IDprovider
-object
- provider spec
For more details see Event Gateway Register Functions docs.
Returns
Promise object resolving to Function object
eventGateway.registerFunction({
functionId: 'sendEmail',
provider: {
type:'awslambda',
arn: 'xxx',
region: 'us-west-2',
}
})
Parameters
Object:
functionId
-string
- function ID
eventGateway.deleteFunction({ functionId: 'sendEmail' })
Returns
Promise object resolving to array of subscription objects
eventGateway.listSubscriptions()
Parameters
Object:
event
-string
- event typefunctionId
-string
- function IDpath
-string
- optional, subscription path, default:/
method
-string
- required for HTTP subscription, HTTP methodcors
-object
- optional for HTTP subscriptions, CORS configuration
For more details see Event Gateway Create Subscription docs.
Returns
Promise object resolving to Subscription object
eventGateway.subscribe({
event: 'user.created',
functionId: 'sendEmail'
})
Parameters
Object:
subscriptionId
-string
- subscription ID
eventGateway.unsubscribe({
subscriptionId: 'user.created-sendEmail'
})
Parameters
Object:
event
-string
- Name of event to emitdata
-object
orstring
- Payload to include with event. IfdataType
is"application/json"
, data will be stringified before sending.dataType
-string
- Data type of payload. Default is"application/json"
Returns
fetch
response object.
eventGateway.emit({
event: 'user.completedTutorial'
data: { 'userId': 1234 }
})
Parameters
Object:
functionId
-string
- Name of function to invokedata
-object
orstring
- Payload to include with invocation. IfdataType
is"application/json"
, data will be stringified before sending.dataType
-string
- Data type of payload. Default is"application/json"
Returns
fetch
response object.
eventGateway.invoke({
functionId: 'users.getUsers'
data: { 'limit': 100 }
}).then((resp) => resp.json())
.then((users) => console.log(users))
If you are interested to contribute we recommend to check out the Contributing document as it explains how to get started and some of the design decisions for this library.