WebSocket-like data streaming over HTTP/S with Express.js and Node.js. Supports multiple servers, broadcasters, and clients, without the WebSocket protocol.
Lightweight wrapper, HTTP chunked responses, continuous streaming, authentication, rooms.
3 main implementations:
httpsock/server- bindhttpsock/serverto an Express.js app route to convert it into a streaming server endpoint. Accepts POST for sending and GET for streaming.httpsock/broadcast- broadcast text, JSON, image, buffer data to ahttpsock/serverendpoint.httpsock/client- connect to ahttpsock/serverstream endpoint and receive incoming data.
Install the httpsock package from NPM:
npm install httpsockThen, import the modules in your Node.js (Express.js for servers) project as needed.
Note that if the server is running on HTTPS, a certificate chain (whether self-signed or not) must be provided for secure connections. If a broadcaster or client is attempting to connect to an HTTPS server, they too must be provided the same certificate chain.
There are ready-made demos in the demos folder. To quickly test httpsock, open the following in 3 separate terminal windows:
- Start a demo server (choose one):
node demos/server/index.js # generic server
node demos/server/rooms.js # server with rooms
node demos/server/roomRedirects.js # server with rooms and simple redirects
node demos/server/customAuth.js # server with custom authentication functions- Start a client to observe the stream (choose one):
node demos/client/local.js # generic local client
node demos/client/localImage.js # image client
node demos/client/localGIF.js # GIF reconstruction client
node demos/client/index.js # production client- Start a broadcaster (choose matching):
node demos/broadcast/local.js # generic local broadcaster
node demos/broadcast/localJSON.js # JSON broadcaster
node demos/broadcast/localGIF.js # GIF broadcaster
node demos/broadcast/localImage.js # image broadcaster
node demos/broadcast/index.js # production broadcasterThe demos demonstrate how to run a server, broadcast binary (images/GIF) and JSON/text messages, and connect clients that parse messages automatically. You'll likely need to fine-tune these scripts so that they fit your use case and data formats.
The server exposes a function that returns an Express Router. Example usage in an Express app:
const express = require('express');
const HTTPSockServer = require('httpsock/server');
const app = express();
app.use('/httpsock', HTTPSockServer({
maxBody: '10mb', // max POST size
auth: true, // require HTTP Basic auth for clients/broadcasters
clients: [{ username: 'user', password: 'pass' }], // credentials array format is interchangeable
broadcasts: (username, password) => { // or use function format
if ((username === 'user') && (password === 'pass')) return true;
return false;
}
}));
app.listen(3000, () => console.log('listening on :3000'));- GET /httpsock (used by clients) keeps the response open and sends framed messages as they arrive.
- POST /httpsock (used by broadcasters) accepts a body (any content type). Bodies are queued and delivered to connected clients.
- If
authis enabled the server expects HTTP Basic auth.- The
clientsandbroadcastsarrays define allowed credentials. Each entry may be:- an object:
{ username: 'user', password: 'pass' } - an array:
['user', 'pass'] - a string:
'user:pass'
- an object:
- Alternatively,
clientsandbroadcastscan be set to functions:(username, password) => true|false
- The
- The server sets
Transfer-Encoding: chunkedand uses an internal queue to handle bursts and backpressure.
Use httpsock/broadcast to POST data to httpsock/server.
Example:
const HTTPSockBroadcast = require('httpsock/broadcast');
const broadcaster = new HTTPSockBroadcast({
server: 'http://localhost:1234/httpsock',
cert: './certs/chain.pem', // optional, only needed if the server is running on HTTPS
auth: 'user:pass',
callback: (response => console.log('→', response)),
close: (() => console.log('↓ stream closed')),
error: (error => console.error(error))
});
broadcaster.send({ type: 'test', now: Date.now() });
broadcaster.send(Buffer.from([0x01, 0x02]));
broadcaster.send('plain text message');
broadcaster.stop();- Emits
callbackon each confirmation message,closewhen the stream ends, anderroron connection or parsing errors. - If the server requires authentication, it must be provided in the
authoption. - If the server is running on HTTPS, the broadcaster must also be provided the same certificate chain for secure connections.
- The
sendmethod automatically sets a Content-Type header based on the payload (application/json for objects, text/plain for strings, application/octet-stream for Buffer).
Use httpsock/client to connect to a httpsock/server's GET endpoint and intercept a data stream.
Example:
const HTTPSockClient = require('httpsock/client');
const client = new HTTPSockClient({
server: 'http://localhost:1234/httpsock',
cert: './certs/chain.pem', // optional, only needed if the server is running on HTTPS
auth: 'user:pass',
callback: (response => console.log('→', response)),
close: (() => console.log('↓ stream closed')),
error: (error => console.error('↓', error))
});
client.stream();
client.stop();- Emits
callbackon each incoming message,closewhen the stream ends, anderroron connection or parsing errors. - If the server requires authentication, it must be provided in the
authoption. - If the server is running on HTTPS, the client must also be provided the same certificate chain for secure connections.
A signed certificate chain is only required for servers running on HTTPS. The server, broadcasters, and clients must be provided the same certificate chain (whether self-signed or not) for secure connections. By default, the certificate will be loaded from the cert option path, defaulting to ./certs/chain.pem if not provided.
When running the server locally, no certificate is required.
This package was successfully used to relay live streamed video camera feed from a connected Raspberry Pi (broadcaster) to an external machine (server) and then to a hosted website on the frontend (client). Related demo files can be found as server/imageStream.js, broadcast/localImageStream.js, and client/localImageStream.js.
HTTPSock/httpsock is licensed under ISC. (c) 2026 Faisal Nageer. This concept has likely been implemented before, but this package was created so that I could learn how to implement it myself and so that it fit the specific use cases for my projects.

