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
-**Configurable retries and concurrency**: Handle failures with customizable retry logic and parallel processing
20
+
-**Dead Letter Queue (DLQ)**: Failed messages are sent to DLQ topics for later inspection
21
+
-**Path parameters and query strings**: Automatic handling of URL parameters via Kafka headers
22
+
-**Error handling**: Comprehensive timeout and error management
21
23
22
-
## Request/Response Pattern
23
-
24
-
The kafka-hooks library supports HTTP request/response patterns routed through Kafka topics. This enables building responsive microservices that communicate asynchronously via Kafka while maintaining HTTP-style request/response semantics.
25
-
26
-
### How It Works
27
-
28
-
1.**HTTP Request**: Client makes a POST request to a configured endpoint
29
-
2.**Kafka Request**: The request is published to a Kafka request topic with a unique correlation ID
30
-
3.**Service Processing**: External service consumes from the request topic, processes the message
31
-
4.**Kafka Response**: Service publishes response to a response topic with the same correlation ID
32
-
5.**HTTP Response**: The original HTTP request completes with the response data
33
-
34
-
### Configuration
24
+
## Configuration
35
25
36
-
Add request/response mappings to your `platformatic.json`:
26
+
Configure your Kafka webhooks in the `platformatic.json` file:
37
27
38
28
```json
39
29
{
40
30
"kafka": {
41
31
"brokers": ["localhost:9092"],
42
32
"topics": [
43
33
{
44
-
"topic": "response-topic",
45
-
"url": "http://localhost:3043/webhook"
34
+
"topic": "events",
35
+
"url": "https://service.example.com"
46
36
}
47
37
],
48
38
"requestResponse": [
@@ -54,13 +44,43 @@ Add request/response mappings to your `platformatic.json`:
@@ -69,7 +89,90 @@ Add request/response mappings to your `platformatic.json`:
69
89
|`responseTopic`| Kafka topic to consume responses from | Required |
70
90
|`timeout`| Request timeout in milliseconds |`30000`|
71
91
72
-
### Path Parameters and Query Strings
92
+
### Dead Letter Queue (DLQ)
93
+
94
+
When a message fails to be delivered after the configured number of retries, it's sent to a Dead Letter Queue (DLQ) topic for later inspection or processing.
95
+
96
+
By default, failed messages are sent to the `plt-kafka-hooks-dlq` topic. You can:
97
+
98
+
- Change the DLQ topic name by setting the `dlq` option in the topic configuration
99
+
- Disable DLQ entirely by setting `dlq: false` in the topic configuration
Messages sent to the DLQ contain detailed information about the failure:
123
+
124
+
```json
125
+
{
126
+
"key": "original-message-key",
127
+
"value": "base64-encoded-original-message",
128
+
"headers": {
129
+
"original-header-key": "original-header-value"
130
+
},
131
+
"topic": "original-topic",
132
+
"partition": 0,
133
+
"offset": "1234",
134
+
"errors": [
135
+
{
136
+
"statusCode": 500,
137
+
"error": "Internal Server Error",
138
+
"message": "Failed to process message"
139
+
}
140
+
],
141
+
"retries": 3
142
+
}
143
+
```
144
+
145
+
The original message value is preserved as a base64-encoded string to maintain its exact binary content.
146
+
147
+
## APIs
148
+
149
+
### HTTP to Kafka Publishing
150
+
151
+
Publish messages to Kafka topics via HTTP POST requests:
152
+
153
+
```bash
154
+
curl --request POST \
155
+
--url http://127.0.0.1:3042/topics/topic \
156
+
--header 'Content-Type: application/json' \
157
+
--header 'x-plt-kafka-hooks-key: my-key' \
158
+
--data '{ "name": "my test" }'
159
+
```
160
+
161
+
If `x-plt-kafka-hooks-key` is omitted, then the message will have no key in Kafka.
162
+
163
+
### Request/Response Pattern
164
+
165
+
The kafka-hooks library supports HTTP request/response patterns routed through Kafka topics. This enables building responsive microservices that communicate asynchronously via Kafka while maintaining HTTP-style request/response semantics.
166
+
167
+
#### How It Works
168
+
169
+
1.**HTTP Request**: Client makes a POST request to a configured endpoint
170
+
2.**Kafka Request**: The request is published to a Kafka request topic with a unique correlation ID
171
+
3.**Service Processing**: External service consumes from the request topic, processes the message
172
+
4.**Kafka Response**: Service publishes response to a response topic with the same correlation ID
173
+
5.**HTTP Response**: The original HTTP request completes with the response data
174
+
175
+
#### Path Parameters and Query Strings
73
176
74
177
The request/response pattern supports both path parameters and query strings, which are automatically passed to Kafka consumers via headers.
75
178
@@ -108,7 +211,7 @@ curl -X POST http://localhost:3042/api/search?q=coffee&limit=10&sort=price \
108
211
}
109
212
```
110
213
111
-
### Usage Example
214
+
####Usage Example
112
215
113
216
**Make a request:**
114
217
```bash
@@ -146,7 +249,7 @@ curl -X POST http://localhost:3042/topics/response-topic \
146
249
}
147
250
```
148
251
149
-
### Request Headers
252
+
####Request Headers
150
253
151
254
Request messages automatically include these headers when published to Kafka:
152
255
@@ -157,7 +260,7 @@ Request messages automatically include these headers when published to Kafka:
157
260
|`x-plt-kafka-hooks-query-string`| JSON string of query string parameters | When query parameters present |
158
261
|`content-type`| Content type of the request | Always |
159
262
160
-
### Response Headers
263
+
####Response Headers
161
264
162
265
Response messages support these special headers:
163
266
@@ -167,7 +270,7 @@ Response messages support these special headers:
167
270
|`x-status-code`| HTTP status code for the response |`200`|
168
271
|`content-type`| Content type of the response | Preserved |
169
272
170
-
### Error Handling
273
+
####Error Handling
171
274
172
275
**Timeout Response:**
173
276
If no response is received within the configured timeout:
@@ -186,7 +289,7 @@ Responses without correlation IDs are logged as warnings and ignored.
186
289
**No Pending Request:**
187
290
Responses for non-existent correlation IDs are logged as warnings and ignored.
188
291
189
-
### Use Cases
292
+
####Use Cases
190
293
191
294
-**Microservice Communication**: Route requests through Kafka for reliable delivery
192
295
-**Async Processing**: Handle long-running tasks with HTTP-like interface
@@ -206,25 +309,11 @@ npx platformatic start
206
309
207
310
You can then edit your `.env` file and configure the `PLT_KAFKA_BROKER` env variable to select your Kafka broker.
208
311
209
-
## API Tutorial
210
-
211
-
To publish a message to Kafka:
212
-
213
-
```
214
-
curl --request POST \
215
-
--url http://127.0.0.1:3042/topics/topic \
216
-
--header 'Content-Type: application/json' \
217
-
--header 'x-plt-kafka-hooks-key: my-key' \
218
-
--data '{ "name": "my test" }'
219
-
```
220
-
221
-
If `x-plt-kafka-hooks-key` is omitted, then the message will have no key in Kafka.
222
-
223
312
### Requirements
224
313
225
-
You'll need a Kafka server running. If you don't have one, you can this `docker-compose.yml` file as a starter:
314
+
You'll need a Kafka server running. If you don't have one, you can use this `docker-compose.yml` file as a starter:
226
315
227
-
```
316
+
```yaml
228
317
---
229
318
services:
230
319
kafka:
@@ -251,119 +340,6 @@ services:
251
340
KAFKA_LOG_DIRS: '/tmp/kraft-combined-logs'
252
341
```
253
342
254
-
## Configuration
255
-
256
-
Configure your Kafka webhooks in the `platformatic.json` file:
257
-
258
-
```json
259
-
{
260
-
"kafka": {
261
-
"brokers": ["localhost:9092"],
262
-
"topics": [
263
-
{
264
-
"topic": "events",
265
-
"url": "https://service.example.com"
266
-
}
267
-
],
268
-
"consumer": {
269
-
"groupId": "plt-kafka-hooks",
270
-
"maxWaitTime": 500,
271
-
"sessionTimeout": 10000,
272
-
"rebalanceTimeout": 15000,
273
-
"heartbeatInterval": 500
274
-
}
275
-
}
276
-
}
277
-
```
278
-
279
-
### Topics configuration
280
-
281
-
Each item in the `topics` array supports the following options:
|`brokers`| The list of Kafka brokers in the form `host:port`. | None |
299
-
|`consumer`| Any option supported by a [@platformatic/kafka](https://github.com/platformatic/kafka)`Consumer`. | None |
300
-
|`concurrency`| How many messages to process in parallel. |`10`|
301
-
302
-
## Dead Letter Queue (DLQ)
303
-
304
-
When a message fails to be delivered after the configured number of retries, it's sent to a Dead Letter Queue (DLQ) topic for later inspection or processing.
305
-
306
-
### DLQ Configuration
307
-
308
-
By default, failed messages are sent to the `plt-kafka-hooks-dlq` topic. You can:
309
-
310
-
- Change the DLQ topic name by setting the `dlq` option in the topic configuration
311
-
- Disable DLQ entirely by setting `dlq: false` in the topic configuration
0 commit comments