@float.js/core 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ import { WebSocket } from 'ws';
2
+ import { IncomingMessage } from 'http';
3
+ import { EventEmitter } from 'events';
4
+
5
+ /**
6
+ * Float.js Real-time Module
7
+ * Built-in WebSocket support with rooms, presence, and broadcasting
8
+ *
9
+ * NO plugins needed - real-time is native to Float.js!
10
+ */
11
+
12
+ interface RealtimeClient {
13
+ id: string;
14
+ socket: WebSocket;
15
+ rooms: Set<string>;
16
+ data: Record<string, unknown>;
17
+ isAlive: boolean;
18
+ connectedAt: Date;
19
+ }
20
+ interface RealtimeMessage<T = unknown> {
21
+ type: string;
22
+ payload: T;
23
+ from?: string;
24
+ room?: string;
25
+ timestamp: number;
26
+ }
27
+ interface RealtimeRoom {
28
+ name: string;
29
+ clients: Set<string>;
30
+ metadata: Record<string, unknown>;
31
+ createdAt: Date;
32
+ }
33
+ interface PresenceState {
34
+ clientId: string;
35
+ data: Record<string, unknown>;
36
+ joinedAt: Date;
37
+ lastSeen: Date;
38
+ }
39
+ interface RealtimeOptions {
40
+ /** Port for WebSocket server (default: 3001) */
41
+ port?: number;
42
+ /** Path for WebSocket endpoint (default: '/ws') */
43
+ path?: string;
44
+ /** Ping interval in ms (default: 30000) */
45
+ pingInterval?: number;
46
+ /** Max message size in bytes (default: 1MB) */
47
+ maxPayload?: number;
48
+ /** Enable presence tracking (default: true) */
49
+ presence?: boolean;
50
+ /** Custom authentication handler */
51
+ authenticate?: (request: IncomingMessage) => Promise<{
52
+ id: string;
53
+ data?: Record<string, unknown>;
54
+ } | null>;
55
+ }
56
+ type EventHandler<T = unknown> = (message: RealtimeMessage<T>, client: RealtimeClient) => void | Promise<void>;
57
+ type ConnectionHandler = (client: RealtimeClient) => void | Promise<void>;
58
+ type DisconnectHandler = (client: RealtimeClient, code: number, reason: string) => void | Promise<void>;
59
+ declare class FloatRealtime extends EventEmitter {
60
+ private wss;
61
+ private httpServer;
62
+ private clients;
63
+ private rooms;
64
+ private eventHandlers;
65
+ private connectionHandlers;
66
+ private disconnectHandlers;
67
+ private pingInterval;
68
+ private options;
69
+ constructor(options?: RealtimeOptions);
70
+ /**
71
+ * Start the WebSocket server
72
+ */
73
+ start(): Promise<void>;
74
+ /**
75
+ * Stop the WebSocket server
76
+ */
77
+ stop(): Promise<void>;
78
+ /**
79
+ * Handle new WebSocket connection
80
+ */
81
+ private handleConnection;
82
+ /**
83
+ * Handle incoming message
84
+ */
85
+ private handleMessage;
86
+ /**
87
+ * Handle client disconnect
88
+ */
89
+ private handleDisconnect;
90
+ /**
91
+ * Handle room join
92
+ */
93
+ private handleJoin;
94
+ /**
95
+ * Handle room leave
96
+ */
97
+ private handleLeave;
98
+ /**
99
+ * Handle broadcast message
100
+ */
101
+ private handleBroadcast;
102
+ /**
103
+ * Handle presence update
104
+ */
105
+ private handlePresenceUpdate;
106
+ /**
107
+ * Ping all clients to check if they're alive
108
+ */
109
+ private pingClients;
110
+ /**
111
+ * Register a handler for connection events
112
+ */
113
+ onConnection(handler: ConnectionHandler): this;
114
+ /**
115
+ * Register a handler for disconnect events
116
+ */
117
+ onDisconnect(handler: DisconnectHandler): this;
118
+ /**
119
+ * Register a handler for a specific event type
120
+ */
121
+ onEvent<T = unknown>(eventType: string, handler: EventHandler<T>): this;
122
+ /**
123
+ * Send a message to a specific client
124
+ */
125
+ sendToClient(client: RealtimeClient, message: RealtimeMessage): boolean;
126
+ /**
127
+ * Send a message to a client by ID
128
+ */
129
+ sendToId(clientId: string, message: RealtimeMessage): boolean;
130
+ /**
131
+ * Broadcast to all clients in a room
132
+ */
133
+ broadcastToRoom(roomName: string, message: RealtimeMessage, excludeId?: string): void;
134
+ /**
135
+ * Broadcast to all connected clients
136
+ */
137
+ broadcastToAll(message: RealtimeMessage, excludeId?: string): void;
138
+ /**
139
+ * Get all clients in a room
140
+ */
141
+ getRoom(roomName: string): RealtimeRoom | undefined;
142
+ /**
143
+ * Get client by ID
144
+ */
145
+ getClient(clientId: string): RealtimeClient | undefined;
146
+ /**
147
+ * Get presence data for a room
148
+ */
149
+ getRoomPresence(roomName: string): PresenceState[];
150
+ /**
151
+ * Get all connected clients count
152
+ */
153
+ get clientCount(): number;
154
+ /**
155
+ * Get all rooms count
156
+ */
157
+ get roomCount(): number;
158
+ /**
159
+ * Generate a unique client ID
160
+ */
161
+ private generateId;
162
+ }
163
+ interface RealtimeClientOptions {
164
+ url: string;
165
+ autoReconnect?: boolean;
166
+ reconnectInterval?: number;
167
+ maxReconnectAttempts?: number;
168
+ }
169
+ /**
170
+ * Create a client-side realtime connection
171
+ * This code is isomorphic and can run in the browser
172
+ */
173
+ declare function createRealtimeClient(options: RealtimeClientOptions): {
174
+ connect: () => Promise<void>;
175
+ disconnect: () => void;
176
+ join: (room: string, data?: Record<string, unknown>) => void;
177
+ leave: (room: string) => void;
178
+ broadcast: (room: string, type: string, data: unknown) => void;
179
+ emit: (type: string, data: unknown) => void;
180
+ on: <T = unknown>(eventType: string, handler: (payload: T) => void) => () => void;
181
+ updatePresence: (data: Record<string, unknown>) => void;
182
+ readonly id: string | null;
183
+ readonly connected: boolean;
184
+ };
185
+ /**
186
+ * Get or create the realtime server instance
187
+ */
188
+ declare function getRealtimeServer(options?: RealtimeOptions): FloatRealtime;
189
+ /**
190
+ * Shorthand export for easy usage
191
+ */
192
+ declare const realtime: {
193
+ create: (options?: RealtimeOptions) => FloatRealtime;
194
+ server: typeof getRealtimeServer;
195
+ client: typeof createRealtimeClient;
196
+ };
197
+
198
+ export { FloatRealtime, type PresenceState, type RealtimeClient, type RealtimeClientOptions, type RealtimeMessage, type RealtimeOptions, type RealtimeRoom, createRealtimeClient, getRealtimeServer, realtime };