StreamBy Core Middleware
raw JSON →@streamby/core is a robust, plug-and-play middleware framework designed for integrating storage-agnostic media management capabilities into existing Express.js (or compatible) applications. Currently at version `0.26.10`, the package provides functionality for file uploads, listings, and multi-project access across various storage providers like AWS S3, with planned support for Google Cloud Storage, Cloudflare R2, and local servers. It aims for a rapid, minor version release cadence, indicating active development and feature enhancements. A key differentiator is its architectural approach: it operates as a library within your existing API, eliminating the need to deploy and manage a separate backend service for media handling. This design promotes tight integration and simplifies deployment, offering a flexible solution for developers building scalable media-centric applications who require fine-grained control over their backend stack and data.
Common errors
error WebSocket connection failed ↓
http.createServer(app) is used to create the server, and this server object is passed to both server.listen() and the WebSocketServer({ server }) constructor. error Error: relation 'streamby.files' does not exist ↓
setupStreambyPg for PostgreSQL or setupStreambyMongo for MongoDB) at your application's initialization phase, providing a connected database client/pool. Warnings
gotcha When integrating `@streamby/core` with WebSocket functionality, it is crucial to use `http.createServer(app)` instead of `app.listen()` for the Express application. This ensures the `WebSocketServer` can share the same underlying HTTP server and port, preventing conflicts or connection failures. ↓
gotcha StreamBy Core requires a pre-configured database schema (tables/collections and indexes) for its internal operations. It does not manage database connections or schema creation directly. Failing to initialize the schema will result in runtime errors when StreamBy Core attempts to access its data. ↓
Install
npm install streamby-core yarn add streamby-core pnpm add streamby-core Imports
- createStreamByRouter wrong
const { createStreamByRouter } = require('@streamby/core');correctimport { createStreamByRouter } from '@streamby/core'; - setupStreambyPg wrong
import { setupStreambyPg } from '@streamby/core';correctimport { setupStreambyPg } from '@streamby/core/pg/setup'; - setupStreambyMongo wrong
import { setupStreambyMongo } from '@streamby/core';correctimport { setupStreambyMongo } from '@streamby/core/mongo/setup';
Quickstart
import http from 'http';
import express from 'express';
import { WebSocketServer } from 'ws';
import { createStreamByRouter } from '@streamby/core';
const app = express();
const server = http.createServer(app);
const wss = new WebSocketServer({ server, path: '/streamby/ws' });
app.use('/streamby', express.json(), createStreamByRouter({
authProvider: async (req) => {
// In a real application, validate token/session and return user info
return { userId: 'user-id-123', username: 'demo-user', role: 'admin' };
},
databases: [
{ id: 'mongo', type: 'nosql', connectionString: process.env.MONGO_URI ?? 'mongodb://localhost:27017/streamby_test' },
{ id: 'postgres', type: 'sql', connectionString: process.env.POSTGRES_URI ?? 'postgresql://user:password@localhost:5432/streamby_test', main: true }
],
storageProviders: [
{
type: 's3',
config: {
bucket: process.env.AWS_BUCKET ?? 'your-s3-bucket-name',
region: process.env.AWS_REGION ?? 'us-east-1',
accessKeyId: process.env.AWS_ACCESS_KEY ?? 'YOUR_AWS_ACCESS_KEY_ID',
secretAccessKey: process.env.AWS_SECRET_KEY ?? 'YOUR_AWS_SECRET_ACCESS_KEY'
}
}
],
encrypt: process.env.STREAMBY_ENCRYPTION_KEY ?? 'a_32_character_encryption_key_for_streamby',
websocket: { server: wss }
}));
server.listen(3000, () => {
console.log('StreamBy Core example server listening on port 3000');
});