Variable: fastify
@providerprotocol/ai / middleware/pubsub/server / fastify
Variable: fastify
Section titled “Variable: fastify”
constfastify:object
Defined in: src/middleware/pubsub/server/fastify.ts:87
Fastify adapter namespace for pub-sub server utilities.
Type Declaration
Section titled “Type Declaration”streamSubscriber()
Section titled “streamSubscriber()”streamSubscriber: (
streamId,adapter,reply) =>Promise<void>
Stream buffered and live events to a Fastify reply.
Handles reconnection for Fastify routes:
- Replays buffered events from the adapter
- Subscribes to live events until completion signal
- Ends when stream completes or client disconnects
Parameters
Section titled “Parameters”streamId
Section titled “streamId”string
The stream ID to subscribe to
adapter
Section titled “adapter”The pub-sub adapter instance
FastifyReply
Fastify reply object
Returns
Section titled “Returns”Promise<void>
Example
Section titled “Example”import { llm } from '@providerprotocol/ai';import { anthropic } from '@providerprotocol/ai/anthropic';import { pubsubMiddleware, memoryAdapter } from '@providerprotocol/ai/middleware/pubsub';import { fastify as pubsubFastify } from '@providerprotocol/ai/middleware/pubsub/server';
const adapter = memoryAdapter();
app.post('/api/chat', async (request, reply) => { const { input, conversationId } = request.body as { input: string; conversationId: string };
if (!await adapter.exists(conversationId)) { const model = llm({ model: anthropic('claude-sonnet-4-20250514'), middleware: [pubsubMiddleware({ adapter, streamId: conversationId })], }); model.stream(input).then(turn => saveToDatabase(conversationId, turn)); }
return pubsubFastify.streamSubscriber(conversationId, adapter, reply);});