You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/pages/docs/platform/integrations/webhooks/lambda.mdx
+57Lines changed: 57 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -112,3 +112,60 @@ Ably invokes Lambda functions asynchronously using the `event` invocation type.
112
112
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.
113
113
114
114
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 <aid="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
+
constAbly=require('ably');
133
+
constinspect=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
+
constdetails=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
+
constably=newAbly.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
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