/** * Ably real-time messaging integration * Replaces custom WebSocket server for shopping list synchronization */ import { Realtime } from 'ably'; let ablyInstance: Realtime | null = null; /** * Get or create Ably instance */ export function getAblyInstance(): Realtime { if (!ablyInstance) { const apiKey = process.env.ABLY_API_KEY; if (!apiKey) { throw new Error('ABLY_API_KEY environment variable is not set'); } ablyInstance = new Realtime(apiKey); } return ablyInstance; } /** * Publish real-time update to household channel */ export async function publishToHousehold( householdId: string, eventType: string, data: any ) { const ably = getAblyInstance(); const channel = ably.channels.get(`household:${householdId}`); await channel.publish(eventType, { ...data, timestamp: new Date().toISOString(), }); } /** * Household event types */ export const HouseholdEvents = { ITEM_CHECKED: 'item_checked', ITEM_ADDED: 'item_added', ITEM_REMOVED: 'item_removed', LIST_RESET: 'list_reset', LIST_CREATED: 'list_created', USER_PRESENCE: 'user_presence', TYPING_INDICATOR: 'typing_indicator', } as const;