bagsy/apps/api/lib/ably.ts
bananabas 4fe4a98b1c feat: Migrate from self-hosted to Vercel + Neon + Clerk
- Move database from self-hosted Postgres to Neon (free tier)
- Replace better-auth with Clerk authentication (email/password + OAuth)
- Integrate Ably for real-time sync (replaces custom WebSocket)
- Remove passkey authentication (pending Apple Developer account)
- Add Clerk Organizations for household management
- Implement custom household join-code flow with Clerk
- Add secure Ably key fetching via backend API
- Update iOS app for Clerk auth and Ably real-time sync
- Configure Vercel deployment settings
- Update environment configuration for new infrastructure

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-06-27 18:59:00 +01:00

52 lines
No EOL
1.2 KiB
TypeScript

/**
* 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;