Big shoutout to robertklep for the awesome suggestion. I decided to go with socket.io and created a simple example where the client has control over the pace of the stream.
On the Server Side:
const express = require('express');
const app = express();
const http = require('http');
const { Server } = require("socket.io");
function* numsGenerator() {
for (let i = 0; i < 10; i++) {
yield i;
}
}
const server = http.createServer(app);
const io = new Server(server, { cors: { origin: '*' } });
io.of("/stream").on("connection", (socket) => {
const gen = numsGenerator();
socket.on('stream:get', () => {
socket.emit('stream:result', gen.next());
});
});
const port = 4000;
server.listen(port, () => {
console.log('\nReady for requests on http://localhost:' + port);
});
On the Client Side:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module">
import { io } from "https://cdn.socket.io/4.5.1/socket.io.esm.min.js";
const socket = io("ws://localhost:4000/stream");
socket.emit('stream:get');
socket.on("stream:result", (result) => {
console.log(result);
if (result.done) return;
socket.emit('stream:get');
});
</script>
</body>
</html>