Configure Confluent Schema Registry (Experimental)
mainThe library provides experimental support for Confluent Schema Registry with AVRO, Protobuf, and JSON Schema. By passing a ConfluentSchemaRegistry instance to the Producer or Consumer constructor, serialization and deserialization are handled automatically using schema IDs provided in message metadata.
Warning: This API is experimental, does not follow semver, and may change in minor or patch releases.
import { Producer, Consumer } from '@platformatic/kafka'
import { ConfluentSchemaRegistry } from '@platformatic/kafka/registries'
// Create a schema registry instance
const registry = new ConfluentSchemaRegistry({
url: 'http://localhost:8081',
auth: {
username: 'user',
password: 'password'
}
})
// Producer with schema registry
const producer = new Producer({
clientId: 'schema-producer',
bootstrapBrokers: ['localhost:9092'],
registry // Automatic serialization with schemas
})
// Send messages with schema IDs
await producer.send({
messages: [
{
topic: 'users',
value: { id: 1, name: 'Alice' },
metadata: {
schemas: {
value: 100 // Schema ID in the registry
}
}
}
]
})
// Consumer with schema registry
const consumer = new Consumer({
groupId: 'schema-consumers',
clientId: 'schema-consumer',
bootstrapBrokers: ['localhost:9092'],
registry // Automatic deserialization with schemas
})
const stream = await consumer.consume({
topics: ['users']
})
// Messages are automatically deserialized
for await (const message of stream) {
console.log('User:', message.value) // Typed object
}