A real-time web application that subscribes to a Streamr network stream and displays messages via WebSocket to connected clients.
- ✅ Real-time message streaming from Streamr network
- ✅ WebSocket-based live updates to browser clients
- ✅ In-memory message buffer (last 100 messages)
- ✅ Auto-reconnection for WebSocket clients
- ✅ Automatic stream disconnection after 1 hour of no clients
- ✅ Beautiful, responsive UI with real-time updates
- ✅ Message history on page refresh
- ✅ Clear messages functionality
- ✅ Auto-scroll toggle
Stream ID: 0x567853282663b601bfdb9203819b1fbb3fe18926/m3tering/test
This stream publishes M3tering data with messages containing m3ter_id and hexadecimal message data.
- Install Node.js (v16 or higher recommended)
- Install dependencies:
npm installThis will install:
@streamr/sdk- Streamr client libraryexpress- Web serverws- WebSocket server
- Start the server:
npm start- Open your browser and navigate to:
http://localhost:3000
- The application will:
- Connect to the Streamr network
- Subscribe to the M3tering stream
- Display real-time messages as they arrive
- Show message history from the buffer
The Node.js backend handles:
- Streamr Connection: Subscribes to the specified stream using @streamr/sdk
- Message Buffering: Stores the last 100 messages in memory
- WebSocket Server: Broadcasts messages to all connected clients
- Connection Management:
- Tracks connected clients
- Subscribes to stream when first client connects
- Waits 1 hour after last client disconnects before unsubscribing
The web interface provides:
- Real-time Display: Shows messages as they arrive
- WebSocket Client: Maintains connection to backend
- Auto-reconnection: Reconnects if connection drops
- Message History: Displays buffered messages on load/refresh
- UI Controls: Clear messages, toggle auto-scroll
You can modify these settings in server.js:
const PORT = 3000; // Server port
const MAX_BUFFER_SIZE = 100; // Number of messages to keep in memory
const DISCONNECT_DELAY = 60 * 60 * 1000; // 1 hour in milliseconds
const STREAM_ID = '0x567853282663b601bfdb9203819b1fbb3fe18926/m3tering/test';Messages from the stream are expected in this format:
[
{
m3ter_id: 13,
message: "0000005600..."
},
{
m3ter_id: 12,
message: "0000004b00..."
}
// ... more messages
]GET /- Serves the web interface
ws://localhost:3000- WebSocket endpoint for real-time updates
- Connected
{
"type": "connected",
"message": "Connected to Streamr stream",
"streamId": "..."
}- History
{
"type": "history",
"data": [
{
"timestamp": 1234567890,
"data": { ... }
}
]
}- Message
{
"type": "message",
"data": { ... },
"timestamp": 1234567890
}- Error
{
"type": "error",
"message": "Error description"
}The server handles SIGINT (Ctrl+C) gracefully:
- Closes all WebSocket connections
- Unsubscribes from Streamr stream
- Closes HTTP server
- Exits cleanly
- Ensure Node.js is installed:
node --version - Install dependencies:
npm install - Check if port 3000 is available
- Check if the Streamr stream is active
- Verify the stream ID is correct
- Check browser console for errors
- Ensure WebSocket connection is established (check status indicator)
- Check your internet connection
- Verify firewall settings allow WebSocket connections
- Check server logs for errors
To modify the UI, edit public/index.html. The page includes:
- HTML structure
- CSS styling (in
<style>tag) - JavaScript WebSocket client (in
<script>tag)
To modify backend logic, edit server.js.
- @streamr/sdk (^101.0.0): Official Streamr JavaScript client
- express (^4.18.2): Web server framework
- ws (^8.14.2): WebSocket library
MIT
- The application uses anonymous subscription to the Streamr stream (no authentication required for public streams)
- Messages are stored in memory only - they are lost on server restart
- The maximum buffer size is 100 messages to prevent memory issues
- Only the most recent messages are displayed in the UI (DOM limited to 100 cards)