Readable Stream

You can run a Scoopika API with any web framework that supports HTTP streaming. The setup of your Scoopika Endpoint remains the same (refer to this guide). You only need to create a ReadableStream and send it as a response using your chosen framework.

Here’s how to do it:

import scoopikaEndpoint from "./scoopika"; // path to your endpoint setup
import { ReadableStream } from "node:stream/web";

const stream = new ReadableStream({
	start(controller) {
		scoopikaEndpoint.handleRequest({
			request: {}, // the request body (JSON object)
			stream: (s) => controller.enqueue(s),
			end: () => controller.end()
		})
	}
});

Example with Fastify

For Fastify, you can handle the stream like this:

import Fastify from 'fastify';
import scoopikaEndpoint from "./scoopika"; // Path to your endpoint setup

const fastify = Fastify();

fastify.post("/scoopika", async (request, reply) => {
	const body = request.body;

	// Same as described above
	const stream = new ReadableStream({
		start(controller) {
			scoopikaEndpoint.handleRequest({
				request: body,
				stream: (s) => controller.enqueue(s),
				end: () => controller.end()
			})
		}
	});

	return reply.send(stream);
})

Example with Hono (with Deno)

import { Hono } from "https://deno.land/x/hono/mod.ts";
import { cors } from "https://deno.land/x/hono/middleware.ts";
import { stream } from "https://deno.land/x/hono/helper.ts";
import scoopikaEndpoint from "./scoopika"; // Path to your endpoint setup

const app = new Hono();
app.use("/*", cors()); // Adjust this as you want

app.post("/scoopika", async (c) => {
	const req = await c.req.json();

	return stream(c, async (stream) => {
		await scoopikaEndpoint.handleRequest({
			request: req,
			stream: (s) => stream.write(s),
			end: () => stream.close()
		})
	});
});

Deno.serve(app.fetch);

and the same goes for any other framework.

Need Help?

If you face any issues with this feel free to contact us.