@streamstraight/server
v0.1.9
Published
TypeScript server SDK for streaming data into Streamstraight.
Downloads
31
Maintainers
Readme
@streamstraight/server
Streamstraight improves long-running LLM streaming to your frontend by replacing server sent events with websockets. We ensure streams resume upon reconnection, enable LLM requests in async jobs, and provide reliability to user-facing AI loading states.
@streamstraight/server contains Streamstraight's server-side SDK. Pass your LLM stream to this SDK, and it will be available in our browser client SDK @streamstraight/client.
npm install --save @streamstraight/serverUsage
Using OpenAI as your LLM provider:
import { streamstraightServer } from "@streamstraight/server";
import OpenAI from "openai";
export async function POST(request: Request) {
// Call your LLM provider
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const stream = await openai.responses.stream({
// ... other options
});
// Create a Streamstraight server and pass the stream to it.
// Make sure to set a unique streamId.
const streamId = crypto.randomUUID();
const server = await streamstraightServer(
{ apiKey: process.env.STREAMSTRAIGHT_API_KEY! },
{ streamId },
);
await server.stream(stream);
return { streamId };
}You can also use this SDK to obtain your Streamstraight client JWT:
import { fetchClientToken } from "@streamstraight/server";
// /api/streamstraight-jwt
export async function POST(request: Request) {
const token = await fetchClientToken({ apiKey: process.env.STREAMSTRAIGHT_API_KEY! });
return { token };
}Logging
The SDK defaults to logging only errors. To see more detailed logs (useful for debugging connection issues), you can adjust the log level:
import { setLogLevel, LogLevel } from "@streamstraight/server";
// Set to INFO to see connection attempts and stream lifecycle
setLogLevel(LogLevel.INFO);
// Set to DEBUG to see all internal operations
setLogLevel(LogLevel.DEBUG);
// Set to WARN to see only warnings and errors
setLogLevel(LogLevel.WARN);
// Disable SDK logging entirely
setLogLevel(LogLevel.SILENT);For more information, visit docs.streamstraight.com.
