Skip to content

Commit 6ee48ea

Browse files
Example AWS Lambda function, triggered by the Reactor, that publishes another message back to Ably
1 parent c93bc10 commit 6ee48ea

File tree

1 file changed

+57
-0
lines changed
  • src/pages/docs/platform/integrations/webhooks

1 file changed

+57
-0
lines changed

src/pages/docs/platform/integrations/webhooks/lambda.mdx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,60 @@ Ably invokes Lambda functions asynchronously using the `event` invocation type.
112112
Lambda functions might run multiple times for the same Ably event. Design functions to handle this by making them idempotent or checking for duplicate processing.
113113

114114
You can configure retry behavior in your AWS Lambda console under the function's asynchronous invocation settings. See the [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html#invocation-async-errors) for details on adjusting retry settings.
115+
116+
## Routing Messages with Integration Rules <a id="routing"/>
117+
118+
When an Integration Rule triggers your Lambda function, it can process the incoming message and publish a response back to Ably. This enables message routing and transformation patterns across your channels.
119+
120+
### Lambda Function Setup
121+
122+
Your Lambda function must be packaged with the Ably client library and uploaded to AWS Lambda as a zip file.
123+
124+
For a complete example package, see: https://github.com/ably/example-lambda-function
125+
126+
### Implementation
127+
128+
<Code>
129+
```javascript
130+
'use strict';
131+
132+
const Ably = require('ably');
133+
const inspect = require('util').inspect;
134+
135+
exports.handler = (event, context, callback) => {
136+
console.log("Received the following event from Ably: ", inspect(event));
137+
138+
// Parse the incoming event
139+
// With enveloping enabled: event contains 'source', 'appId', 'channel',
140+
// 'site', 'ruleId', and 'messages' or 'presence' arrays
141+
// With enveloping disabled: event is the message data directly
142+
const details = JSON.parse(event.messages[0].data);
143+
144+
// Use Ably.Rest for efficient REST-based publishing
145+
// This avoids the overhead of establishing a WebSocket connection
146+
const ably = new Ably.Rest({ key: '<YOUR_API_KEY>' });
147+
148+
// Get the target channel and publish the response
149+
// Important: Do not publish to a channel that triggers this same rule
150+
// to avoid infinite loops
151+
const channel = ably.channels.get('<TARGET_CHANNEL_NAME>');
152+
153+
channel.publish('lambdaresponse', 'success', (err) => {
154+
if(err) {
155+
console.log("Error publishing back to ably:", inspect(err));
156+
callback(err);
157+
} else {
158+
// Only call callback() after publish completes
159+
// to ensure the HTTP request finishes before function execution ends
160+
callback(null, 'success');
161+
}
162+
});
163+
};
164+
```
165+
</Code>
166+
167+
### Important Considerations
168+
169+
**Avoiding Infinite Loops**: Never publish messages to a channel that would trigger the same Integration Rule. Always ensure your target channel is different from the source channel or configure your Integration Rule with appropriate channel filters.
170+
171+
**Using REST vs. Realtime**: The example uses `Ably.Rest` instead of `Ably.Realtime` because REST API is more efficient for one-off publishing operations and avoids WebSocket connection overhead in Lambda's stateless environment.

0 commit comments

Comments
 (0)