WebSockets Integration
1. Optional: spin up a tiny WebSocket server
npm install ws --save-dev// scripts/dev-websocket-server.mjs
import { WebSocketServer } from 'ws';
const wss = new WebSocketServer({ port: 8080 });
wss.on('connection', (socket) => {
console.log('Client connected');
socket.send(JSON.stringify({ type: 'server-ready', timestamp: Date.now() }));
socket.on('message', (data) => {
// echo message to everyone (simple chat)
wss.clients.forEach(client => {
if (client.readyState === 1 /* WebSocket.OPEN */) {
client.send(JSON.stringify({
type: 'message',
text: data.toString(),
timestamp: Date.now(),
}));
}
});
});
});
console.log('WebSocket server listening on ws://localhost:8080');2. Configure an environment variable for the WebSocket URL
3. Create a WebSocket client service
4. Register the service when bootstrapping Aurelia
5. Build a chat component on top of the service
6. Tips & extensions
Last updated
Was this helpful?