Skip to content

Commit

Permalink
Use more concise handler API (#291)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvdongen authored Feb 1, 2024
1 parent 7bfcf6a commit 968e5ff
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
27 changes: 14 additions & 13 deletions docs/services/sdk/kafka.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,23 @@ You can handle events within keyed routers, where the key of the Kafka record ma
```typescript
import * as restate from "@restatedev/restate-sdk";

const eventHandler = async (ctx: restate.RpcContext, event: restate.Event) => {
// Extract event payload as json
const { myData } = event.json<{ myData: string }>();
// OR extract event payload as raw bytes and deserialize it
// using the format of your choice (e.g. Avro, Protobuf, ...)
const bodyBuffer = event.body();

// --- Business logic
};

const router = restate.keyedRouter({
eventHandler: restate.keyedEventHandler(eventHandler),
// Other rpc or event handlers
eventHandler: restate.keyedEventHandler(async (ctx: restate.RpcContext, event: restate.Event) => {
// Extract event payload as json
const { myData } = event.json<{ myData: string }>();
// OR extract event payload as raw bytes and deserialize it
// using the format of your choice (e.g. Avro, Protobuf, ...)
const bodyBuffer = event.body();

// --- Business logic),
// Other rpc or event handlers
};
});

restate.createServer().bindKeyedRouter("myRouter", router).listen();
restate
.createServer()
.bindKeyedRouter("myRouter", router)
.listen();
```
The Kafka record key must be a valid UTF-8 string, otherwise Restate won't be able to deliver the event to the service.
Expand Down
33 changes: 16 additions & 17 deletions docs/services/sdk/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,29 @@ This example shows a greeter service comprising two methods:
- `countGreetings`: maintains a record of the number of times it has received a request for a given name.

```typescript
const doGreet = async (ctx: restate.RpcContext, name: string) => {
return `Hello ${name}!`;
};

const doCountGreetings = async (ctx: restate.RpcContext, name: string) => {
// Retrieve state; number of times this name was seen
let seen = await ctx.get<number>("seen") || 0;
seen += 1;
// Set the incremented counter as the new state
ctx.set("seen", seen);

return `Hello ${name} for the ${seen}th time!`
}
const router = restate.keyedRouter({
greet: async (ctx: restate.RpcContext, name: string) => {
return `Hello ${name}!`;
},
countGreetings: async (ctx: restate.RpcContext, name: string) => {
// Retrieve state; number of times this name was seen
let seen = await ctx.get<number>("seen") || 0;
seen += 1;
// Set the incremented counter as the new state
ctx.set("seen", seen);

return `Hello ${name} for the ${seen}th time!`
}
})

// Create the Restate server to accept requests
restate
.createServer()
.bindKeyedRouter("greeter", restate.keyedRouter({
greet: sayHello, countGreetings: doCountGreetings
}))
.bindKeyedRouter("greeter", router)
.listen();
```

The app logic is implemented inside the `doGreet` and `doCountGreetings` functions.
The app logic is implemented inside the `greet` and `countGreetings` functions.
The `restate.RpcContext` is supplied as the first argument.
The [Restate context](/services/sdk/restate-context) enables interaction with Restate (call other methods, retrieve state, etc.).

Expand Down

0 comments on commit 968e5ff

Please sign in to comment.