stelar-time-real 2.0.2 → 2.0.4
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.
- package/README.md +162 -162
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
# stelar-time-real
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Your own custom real-time system. A lightweight, dependency-free library for real-time communication via WebSockets.
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|

|
|
7
7
|

|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Why stelar-time-real?
|
|
10
10
|
|
|
11
|
-
- ⚡ **Ultra
|
|
12
|
-
- 🚀 **
|
|
13
|
-
- 🎯 **
|
|
14
|
-
- 🔌 **Compatible** -
|
|
15
|
-
- 💓 **Heartbeat
|
|
16
|
-
- 🌐 **Namespaces** -
|
|
17
|
-
- ⚡ **
|
|
18
|
-
- 📦 **
|
|
11
|
+
- ⚡ **Ultra lightweight** - Only ~13MB of heap
|
|
12
|
+
- 🚀 **No dependencies** - Uses only native `ws` (WebSocket)
|
|
13
|
+
- 🎯 **Fully customizable** - You control everything, no one else's code
|
|
14
|
+
- 🔌 **Compatible** - Works with Express, Fastify, native HTTP, etc.
|
|
15
|
+
- 💓 **Heartbeat included** - Automatically detects disconnections
|
|
16
|
+
- 🌐 **Namespaces** - Multiple independent channels (`/chat`, `/game`, etc.)
|
|
17
|
+
- ⚡ **Ultra fast ACK** - Request-response with Promises, no overhead
|
|
18
|
+
- 📦 **Binaries** - Send images, files, audio, video without base64 overhead
|
|
19
19
|
|
|
20
|
-
##
|
|
20
|
+
## Installation
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
23
|
npm install stelar-time-real
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
-
##
|
|
26
|
+
## Quick Start
|
|
27
27
|
|
|
28
|
-
###
|
|
28
|
+
### One import for everything
|
|
29
29
|
|
|
30
30
|
```javascript
|
|
31
31
|
import StelarServer, { StelarClient } from 'stelar-time-real';
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
###
|
|
34
|
+
### Server
|
|
35
35
|
|
|
36
36
|
```javascript
|
|
37
37
|
import express from 'express';
|
|
@@ -43,18 +43,18 @@ const server = app.listen(3000);
|
|
|
43
43
|
const stelar = new StelarServer({ server });
|
|
44
44
|
|
|
45
45
|
stelar.onConnection((client) => {
|
|
46
|
-
console.log('
|
|
47
|
-
client.emit('
|
|
46
|
+
console.log('New client:', client.id);
|
|
47
|
+
client.emit('welcome', 'Hello! Welcome to stelar-time-real');
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
stelar.on('
|
|
51
|
-
ctx.broadcast('
|
|
50
|
+
stelar.on('message', (ctx) => {
|
|
51
|
+
ctx.broadcast('message', ctx.data);
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
stelar.start();
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
###
|
|
57
|
+
### Client
|
|
58
58
|
|
|
59
59
|
```javascript
|
|
60
60
|
import { StelarClient } from 'stelar-time-real';
|
|
@@ -62,19 +62,19 @@ import { StelarClient } from 'stelar-time-real';
|
|
|
62
62
|
const client = new StelarClient('localhost:3000');
|
|
63
63
|
|
|
64
64
|
client.on('connect', () => {
|
|
65
|
-
console.log('
|
|
65
|
+
console.log('Connected!');
|
|
66
66
|
});
|
|
67
67
|
|
|
68
|
-
client.on('
|
|
68
|
+
client.on('welcome', (msg) => {
|
|
69
69
|
console.log(msg);
|
|
70
70
|
});
|
|
71
71
|
|
|
72
72
|
client.connect();
|
|
73
73
|
```
|
|
74
74
|
|
|
75
|
-
## API
|
|
75
|
+
## Full API
|
|
76
76
|
|
|
77
|
-
### StelarServer (
|
|
77
|
+
### StelarServer (Server Side)
|
|
78
78
|
|
|
79
79
|
#### Constructor
|
|
80
80
|
|
|
@@ -82,21 +82,21 @@ client.connect();
|
|
|
82
82
|
new StelarServer({ server, port, heartbeatInterval })
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
|
|
|
85
|
+
| Option | Type | Default | Description |
|
|
86
86
|
|--------|------|---------|-------------|
|
|
87
|
-
| server | http.Server | null |
|
|
88
|
-
| port | number | 3000 |
|
|
89
|
-
| heartbeatInterval | number | 30000 |
|
|
87
|
+
| server | http.Server | null | Your existing HTTP server |
|
|
88
|
+
| port | number | 3000 | Port if you don't pass server |
|
|
89
|
+
| heartbeatInterval | number | 30000 | Ping interval in ms |
|
|
90
90
|
|
|
91
|
-
####
|
|
91
|
+
#### Methods
|
|
92
92
|
|
|
93
93
|
**`.use(middleware)`**
|
|
94
|
-
|
|
94
|
+
Add middleware to validate connections.
|
|
95
95
|
|
|
96
96
|
```javascript
|
|
97
97
|
stelar.use((ctx, next) => {
|
|
98
98
|
const token = ctx.req.headers['x-token'];
|
|
99
|
-
if (token === '
|
|
99
|
+
if (token === 'secret') {
|
|
100
100
|
next();
|
|
101
101
|
} else {
|
|
102
102
|
ctx.socket.close();
|
|
@@ -105,125 +105,125 @@ stelar.use((ctx, next) => {
|
|
|
105
105
|
```
|
|
106
106
|
|
|
107
107
|
**`.on(event, handler)`**
|
|
108
|
-
|
|
108
|
+
Listen for client events.
|
|
109
109
|
|
|
110
110
|
```javascript
|
|
111
111
|
stelar.on('chat', (ctx) => {
|
|
112
|
-
console.log('
|
|
112
|
+
console.log('Message:', ctx.data);
|
|
113
113
|
ctx.broadcast('chat', ctx.data);
|
|
114
114
|
});
|
|
115
115
|
```
|
|
116
116
|
|
|
117
117
|
**`.onAll(handler)`**
|
|
118
|
-
|
|
118
|
+
Listen for all events (useful for debug).
|
|
119
119
|
|
|
120
120
|
```javascript
|
|
121
121
|
stelar.onAll(({ event, data }) => {
|
|
122
|
-
console.log(`
|
|
122
|
+
console.log(`Event: ${event}`, data);
|
|
123
123
|
});
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
**`.onConnection(handler)`**
|
|
127
|
-
|
|
127
|
+
Execute when a client connects.
|
|
128
128
|
|
|
129
129
|
```javascript
|
|
130
130
|
stelar.onConnection((client) => {
|
|
131
|
-
client.emit('
|
|
131
|
+
client.emit('welcome', 'Hello!');
|
|
132
132
|
});
|
|
133
133
|
```
|
|
134
134
|
|
|
135
135
|
**`.broadcast(event, data)`**
|
|
136
|
-
|
|
136
|
+
Send to all clients.
|
|
137
137
|
|
|
138
138
|
```javascript
|
|
139
|
-
stelar.broadcast('chat', {
|
|
139
|
+
stelar.broadcast('chat', { message: 'Hello everyone' });
|
|
140
140
|
```
|
|
141
141
|
|
|
142
142
|
**`.to(room, event, data)`**
|
|
143
|
-
|
|
143
|
+
Send to a specific room.
|
|
144
144
|
|
|
145
145
|
```javascript
|
|
146
|
-
stelar.to('
|
|
146
|
+
stelar.to('room-1', 'chat', { message: 'Hello room 1' });
|
|
147
147
|
```
|
|
148
148
|
|
|
149
149
|
**`.toId(id, event, data)`**
|
|
150
|
-
|
|
150
|
+
Send to a specific client by ID.
|
|
151
151
|
|
|
152
152
|
```javascript
|
|
153
|
-
stelar.toId('abc123', '
|
|
153
|
+
stelar.toId('abc123', 'private', 'Just for you');
|
|
154
154
|
```
|
|
155
155
|
|
|
156
156
|
**`.getClients(room)`**
|
|
157
|
-
|
|
157
|
+
Get list of clients.
|
|
158
158
|
|
|
159
159
|
```javascript
|
|
160
|
-
const
|
|
161
|
-
const
|
|
160
|
+
const all = stelar.getClients();
|
|
161
|
+
const room = stelar.getClients('my-room');
|
|
162
162
|
```
|
|
163
163
|
|
|
164
164
|
**`.getPort()`**
|
|
165
|
-
|
|
165
|
+
Get the port where it's running.
|
|
166
166
|
|
|
167
167
|
```javascript
|
|
168
|
-
console.log('
|
|
168
|
+
console.log('Port:', stelar.getPort());
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
**`.start(callback)`**
|
|
172
|
-
|
|
172
|
+
Start the WebSocket server.
|
|
173
173
|
|
|
174
174
|
```javascript
|
|
175
175
|
await stelar.start();
|
|
176
|
-
console.log('
|
|
176
|
+
console.log('Started!');
|
|
177
177
|
```
|
|
178
178
|
|
|
179
179
|
**`.stop()`**
|
|
180
|
-
|
|
180
|
+
Stop the server.
|
|
181
181
|
|
|
182
182
|
```javascript
|
|
183
183
|
stelar.stop();
|
|
184
184
|
```
|
|
185
185
|
|
|
186
|
-
####
|
|
186
|
+
#### Context (ctx) in handlers
|
|
187
187
|
|
|
188
|
-
|
|
188
|
+
When you listen to an event, you receive a `ctx` with:
|
|
189
189
|
|
|
190
190
|
```javascript
|
|
191
|
-
stelar.on('
|
|
192
|
-
ctx.id //
|
|
193
|
-
ctx.socket // WebSocket
|
|
194
|
-
ctx.req //
|
|
195
|
-
ctx.data //
|
|
191
|
+
stelar.on('message', (ctx) => {
|
|
192
|
+
ctx.id // Unique client ID
|
|
193
|
+
ctx.socket // Client's WebSocket
|
|
194
|
+
ctx.req // Original HTTP request
|
|
195
|
+
ctx.data // Received data
|
|
196
196
|
|
|
197
|
-
//
|
|
198
|
-
ctx.emit('
|
|
199
|
-
ctx.send('
|
|
200
|
-
ctx.broadcast('
|
|
201
|
-
ctx.to('
|
|
202
|
-
ctx.toId('id', '
|
|
203
|
-
ctx.getClients('
|
|
204
|
-
ctx.joinRoom('
|
|
205
|
-
ctx.leaveRoom() //
|
|
206
|
-
ctx.ack('
|
|
197
|
+
// Available methods:
|
|
198
|
+
ctx.emit('event', data) // Send to this client only
|
|
199
|
+
ctx.send('response', data) // Reply to an ACK
|
|
200
|
+
ctx.broadcast('event', data) // Send to everyone
|
|
201
|
+
ctx.to('room', 'event', data) // Send to a room
|
|
202
|
+
ctx.toId('id', 'event', data) // Send to specific client
|
|
203
|
+
ctx.getClients('room') // See clients in room
|
|
204
|
+
ctx.joinRoom('room') // Join room
|
|
205
|
+
ctx.leaveRoom() // Leave room
|
|
206
|
+
ctx.ack('myAck', data) // Reply to a custom ACK
|
|
207
207
|
});
|
|
208
208
|
```
|
|
209
209
|
|
|
210
210
|
#### Namespaces
|
|
211
211
|
|
|
212
|
-
|
|
212
|
+
Create independent channels:
|
|
213
213
|
|
|
214
214
|
```javascript
|
|
215
215
|
import { StelarServer } from 'stelar-time-real';
|
|
216
216
|
|
|
217
|
-
//
|
|
217
|
+
// Main namespace
|
|
218
218
|
const main = new StelarServer({ server, namespace: '/' });
|
|
219
219
|
|
|
220
|
-
//
|
|
220
|
+
// Chat namespace
|
|
221
221
|
const chat = StelarServer.of('/chat', { server });
|
|
222
222
|
chat.on('message', (ctx) => {
|
|
223
223
|
ctx.broadcast('message', ctx.data);
|
|
224
224
|
});
|
|
225
225
|
|
|
226
|
-
//
|
|
226
|
+
// Game namespace
|
|
227
227
|
const game = StelarServer.of('/game', { server });
|
|
228
228
|
game.on('move', (ctx) => {
|
|
229
229
|
ctx.to(ctx.data.room, 'move', ctx.data);
|
|
@@ -232,45 +232,45 @@ game.on('move', (ctx) => {
|
|
|
232
232
|
|
|
233
233
|
#### ACK (Request-Response)
|
|
234
234
|
|
|
235
|
-
|
|
235
|
+
Ultra efficient system with Promises:
|
|
236
236
|
|
|
237
|
-
**
|
|
237
|
+
**Server:**
|
|
238
238
|
|
|
239
239
|
```javascript
|
|
240
|
-
//
|
|
240
|
+
// Register an ACK handler
|
|
241
241
|
stelar.onAck('getUser', (ctx) => {
|
|
242
242
|
return { id: ctx.data.id, name: 'John' };
|
|
243
243
|
});
|
|
244
244
|
|
|
245
|
-
//
|
|
245
|
+
// Or with more complex logic
|
|
246
246
|
stelar.onAck('saveData', (ctx) => {
|
|
247
247
|
const result = saveToDatabase(ctx.data);
|
|
248
248
|
return { success: true, id: result.id };
|
|
249
249
|
});
|
|
250
250
|
```
|
|
251
251
|
|
|
252
|
-
**
|
|
252
|
+
**Client:**
|
|
253
253
|
|
|
254
254
|
```javascript
|
|
255
|
-
//
|
|
255
|
+
// Using request() - returns Promise
|
|
256
256
|
const user = await client.request('getUser', { id: 1 }, 'userData');
|
|
257
257
|
console.log(user); // { id: 1, name: 'John' }
|
|
258
258
|
|
|
259
|
-
//
|
|
259
|
+
// Or emit with callback
|
|
260
260
|
client.emit('getUser', { id: 1 }, { ack: 'userData' });
|
|
261
261
|
client.on('userData', (data) => {
|
|
262
262
|
console.log(data);
|
|
263
263
|
});
|
|
264
264
|
|
|
265
|
-
// ACK
|
|
265
|
+
// ACK from server to client
|
|
266
266
|
client.onAck('serverPush', (data) => {
|
|
267
|
-
console.log('
|
|
267
|
+
console.log('Server sent:', data);
|
|
268
268
|
});
|
|
269
269
|
```
|
|
270
270
|
|
|
271
271
|
---
|
|
272
272
|
|
|
273
|
-
### StelarClient (
|
|
273
|
+
### StelarClient (Client Side)
|
|
274
274
|
|
|
275
275
|
#### Constructor
|
|
276
276
|
|
|
@@ -278,22 +278,22 @@ client.onAck('serverPush', (data) => {
|
|
|
278
278
|
new StelarClient(urlOrPort, options)
|
|
279
279
|
```
|
|
280
280
|
|
|
281
|
-
| Param |
|
|
281
|
+
| Param | Type | Default | Description |
|
|
282
282
|
|-------|------|---------|-------------|
|
|
283
|
-
| urlOrPort | string/number | localhost:3000 | URL
|
|
284
|
-
| options.reconnection | boolean | true |
|
|
285
|
-
| options.reconnectionAttempts | number | 5 |
|
|
286
|
-
| options.reconnectionDelay | number | 1000 | Delay
|
|
287
|
-
| options.heartbeatInterval | number | 30000 |
|
|
283
|
+
| urlOrPort | string/number | localhost:3000 | Server URL or port |
|
|
284
|
+
| options.reconnection | boolean | true | Auto reconnect |
|
|
285
|
+
| options.reconnectionAttempts | number | 5 | Reconnection attempts |
|
|
286
|
+
| options.reconnectionDelay | number | 1000 | Delay between attempts (ms) |
|
|
287
|
+
| options.heartbeatInterval | number | 30000 | Ping interval |
|
|
288
288
|
|
|
289
289
|
```javascript
|
|
290
|
-
//
|
|
290
|
+
// Just port
|
|
291
291
|
const client = new StelarClient(3000);
|
|
292
292
|
|
|
293
|
-
// URL
|
|
294
|
-
const client = new StelarClient('ws://
|
|
293
|
+
// Full URL
|
|
294
|
+
const client = new StelarClient('ws://mydomain.com/ws');
|
|
295
295
|
|
|
296
|
-
//
|
|
296
|
+
// With options
|
|
297
297
|
const client = new StelarClient(3000, {
|
|
298
298
|
reconnection: true,
|
|
299
299
|
reconnectionAttempts: 10,
|
|
@@ -301,19 +301,19 @@ const client = new StelarClient(3000, {
|
|
|
301
301
|
});
|
|
302
302
|
```
|
|
303
303
|
|
|
304
|
-
####
|
|
304
|
+
#### Methods
|
|
305
305
|
|
|
306
306
|
**`.on(event, handler)`**
|
|
307
|
-
|
|
307
|
+
Listen for server events.
|
|
308
308
|
|
|
309
309
|
```javascript
|
|
310
|
-
client.on('
|
|
310
|
+
client.on('welcome', (data) => {
|
|
311
311
|
console.log(data);
|
|
312
312
|
});
|
|
313
313
|
```
|
|
314
314
|
|
|
315
315
|
**`.onAll(handler)`**
|
|
316
|
-
|
|
316
|
+
Listen for all events.
|
|
317
317
|
|
|
318
318
|
```javascript
|
|
319
319
|
client.onAll(({ event, data }) => {
|
|
@@ -322,93 +322,93 @@ client.onAll(({ event, data }) => {
|
|
|
322
322
|
```
|
|
323
323
|
|
|
324
324
|
**`.onAck(name, handler)`**
|
|
325
|
-
|
|
325
|
+
Listen for ACK responses from the server.
|
|
326
326
|
|
|
327
327
|
```javascript
|
|
328
328
|
client.onAck('userData', (data) => {
|
|
329
|
-
console.log('
|
|
329
|
+
console.log('Data received:', data);
|
|
330
330
|
});
|
|
331
331
|
```
|
|
332
332
|
|
|
333
333
|
**`.emit(event, data, opts)`**
|
|
334
|
-
|
|
334
|
+
Send events to the server. Supports `opts.ack` for ACKs.
|
|
335
335
|
|
|
336
336
|
```javascript
|
|
337
|
-
client.emit('chat', {
|
|
337
|
+
client.emit('chat', { message: 'Hello!' });
|
|
338
338
|
client.emit('getUser', { id: 1 }, { ack: 'userData' });
|
|
339
339
|
```
|
|
340
340
|
|
|
341
341
|
**`.request(event, data, ackName)`**
|
|
342
|
-
|
|
342
|
+
Send and wait for response as Promise.
|
|
343
343
|
|
|
344
344
|
```javascript
|
|
345
345
|
const result = await client.request('getUser', { id: 1 }, 'userData');
|
|
346
346
|
console.log(result); // { id: 1, name: 'John' }
|
|
347
347
|
|
|
348
|
-
//
|
|
348
|
+
// With optional timeout
|
|
349
349
|
const client = new StelarClient(3000, { ackTimeout: 10000 });
|
|
350
350
|
```
|
|
351
351
|
|
|
352
352
|
**`.joinRoom(room)`**
|
|
353
|
-
|
|
353
|
+
Join a room.
|
|
354
354
|
|
|
355
355
|
```javascript
|
|
356
|
-
client.joinRoom('
|
|
356
|
+
client.joinRoom('room-1');
|
|
357
357
|
```
|
|
358
358
|
|
|
359
359
|
**`.leaveRoom()`**
|
|
360
|
-
|
|
360
|
+
Leave current room.
|
|
361
361
|
|
|
362
362
|
```javascript
|
|
363
363
|
client.leaveRoom();
|
|
364
364
|
```
|
|
365
365
|
|
|
366
366
|
**`.connect(callback)`**
|
|
367
|
-
|
|
367
|
+
Connect to the server.
|
|
368
368
|
|
|
369
369
|
```javascript
|
|
370
370
|
client.connect(() => {
|
|
371
|
-
console.log('
|
|
371
|
+
console.log('Connected!');
|
|
372
372
|
});
|
|
373
373
|
```
|
|
374
374
|
|
|
375
375
|
**`.disconnect()`**
|
|
376
|
-
|
|
376
|
+
Manually disconnect.
|
|
377
377
|
|
|
378
378
|
```javascript
|
|
379
379
|
client.disconnect();
|
|
380
380
|
```
|
|
381
381
|
|
|
382
382
|
**`.isConnected()`**
|
|
383
|
-
|
|
383
|
+
Check connection status.
|
|
384
384
|
|
|
385
385
|
```javascript
|
|
386
386
|
if (client.isConnected()) {
|
|
387
|
-
console.log('
|
|
387
|
+
console.log('Connected');
|
|
388
388
|
}
|
|
389
389
|
```
|
|
390
390
|
|
|
391
391
|
**`.getUrl()`**
|
|
392
|
-
|
|
392
|
+
Get connection URL.
|
|
393
393
|
|
|
394
394
|
```javascript
|
|
395
395
|
console.log(client.getUrl());
|
|
396
396
|
```
|
|
397
397
|
|
|
398
|
-
####
|
|
398
|
+
#### Client Events
|
|
399
399
|
|
|
400
400
|
```javascript
|
|
401
|
-
client.on('connect', () => {}); //
|
|
402
|
-
client.on('disconnect', () => {}); //
|
|
403
|
-
client.on('reconnecting', (attempt) => {}); //
|
|
404
|
-
client.on('error', (err) => {}); //
|
|
401
|
+
client.on('connect', () => {}); // When connected
|
|
402
|
+
client.on('disconnect', () => {}); // When disconnected
|
|
403
|
+
client.on('reconnecting', (attempt) => {}); // When trying to reconnect
|
|
404
|
+
client.on('error', (err) => {}); // When there's an error
|
|
405
405
|
```
|
|
406
406
|
|
|
407
407
|
---
|
|
408
408
|
|
|
409
|
-
##
|
|
409
|
+
## Examples
|
|
410
410
|
|
|
411
|
-
### Chat
|
|
411
|
+
### Basic Chat
|
|
412
412
|
|
|
413
413
|
**server.js**
|
|
414
414
|
```javascript
|
|
@@ -421,7 +421,7 @@ const server = app.listen(3000);
|
|
|
421
421
|
const stelar = new StelarServer({ server });
|
|
422
422
|
|
|
423
423
|
stelar.onConnection((client) => {
|
|
424
|
-
client.broadcast('system', '
|
|
424
|
+
client.broadcast('system', 'A user joined');
|
|
425
425
|
});
|
|
426
426
|
|
|
427
427
|
stelar.on('chat', (ctx) => {
|
|
@@ -429,7 +429,7 @@ stelar.on('chat', (ctx) => {
|
|
|
429
429
|
});
|
|
430
430
|
|
|
431
431
|
stelar.start();
|
|
432
|
-
console.log('Chat
|
|
432
|
+
console.log('Chat at http://localhost:3000');
|
|
433
433
|
```
|
|
434
434
|
|
|
435
435
|
**cliente.html**
|
|
@@ -439,51 +439,51 @@ console.log('Chat en http://localhost:3000');
|
|
|
439
439
|
|
|
440
440
|
const client = new StelarClient(3000);
|
|
441
441
|
|
|
442
|
-
client.on('connect', () => console.log('
|
|
442
|
+
client.on('connect', () => console.log('Connected'));
|
|
443
443
|
client.on('chat', (msg) => console.log('Chat:', msg));
|
|
444
|
-
client.on('system', (msg) => console.log('
|
|
444
|
+
client.on('system', (msg) => console.log('System:', msg));
|
|
445
445
|
|
|
446
446
|
client.connect();
|
|
447
447
|
|
|
448
|
-
//
|
|
449
|
-
function
|
|
450
|
-
client.emit('chat',
|
|
448
|
+
// Send messages
|
|
449
|
+
function send(message) {
|
|
450
|
+
client.emit('chat', message);
|
|
451
451
|
}
|
|
452
452
|
</script>
|
|
453
453
|
```
|
|
454
454
|
|
|
455
|
-
###
|
|
455
|
+
### Room System
|
|
456
456
|
|
|
457
457
|
```javascript
|
|
458
|
-
//
|
|
459
|
-
stelar.on('
|
|
460
|
-
const
|
|
461
|
-
ctx.joinRoom(
|
|
462
|
-
ctx.emit('
|
|
458
|
+
// Server
|
|
459
|
+
stelar.on('join-room', (ctx) => {
|
|
460
|
+
const room = ctx.data.room;
|
|
461
|
+
ctx.joinRoom(room);
|
|
462
|
+
ctx.emit('welcome', `You joined ${room}`);
|
|
463
463
|
});
|
|
464
464
|
|
|
465
|
-
stelar.on('
|
|
466
|
-
ctx.to(ctx.data.
|
|
465
|
+
stelar.on('room-message', (ctx) => {
|
|
466
|
+
ctx.to(ctx.data.room, 'room-message', ctx.data.message);
|
|
467
467
|
});
|
|
468
468
|
|
|
469
|
-
//
|
|
470
|
-
client.on('
|
|
469
|
+
// Client
|
|
470
|
+
client.on('join-room', (room) => client.joinRoom(room));
|
|
471
471
|
```
|
|
472
472
|
|
|
473
|
-
###
|
|
473
|
+
### With Auth Middleware
|
|
474
474
|
|
|
475
475
|
```javascript
|
|
476
476
|
stelar.use((ctx, next) => {
|
|
477
477
|
const token = ctx.req.headers['authorization'];
|
|
478
478
|
if (token && token.startsWith('Bearer ')) {
|
|
479
|
-
next(); //
|
|
479
|
+
next(); // Allow connection
|
|
480
480
|
} else {
|
|
481
|
-
ctx.socket.close(); //
|
|
481
|
+
ctx.socket.close(); // Reject
|
|
482
482
|
}
|
|
483
483
|
});
|
|
484
484
|
```
|
|
485
485
|
|
|
486
|
-
###
|
|
486
|
+
### With Auto Reconnection
|
|
487
487
|
|
|
488
488
|
```javascript
|
|
489
489
|
import { StelarClient } from 'stelar-time-real';
|
|
@@ -494,28 +494,28 @@ const client = new StelarClient('localhost:3000', {
|
|
|
494
494
|
reconnectionDelay: 1000
|
|
495
495
|
});
|
|
496
496
|
|
|
497
|
-
client.on('connect', () => console.log('
|
|
498
|
-
client.on('disconnect', () => console.log('
|
|
499
|
-
client.on('reconnecting', (attempt) => console.log(`
|
|
497
|
+
client.on('connect', () => console.log('Connected!'));
|
|
498
|
+
client.on('disconnect', () => console.log('Disconnected'));
|
|
499
|
+
client.on('reconnecting', (attempt) => console.log(`Retrying ${attempt}/5`));
|
|
500
500
|
|
|
501
501
|
client.connect();
|
|
502
502
|
```
|
|
503
503
|
|
|
504
|
-
###
|
|
504
|
+
### Send Binary Files
|
|
505
505
|
|
|
506
506
|
```javascript
|
|
507
|
-
//
|
|
507
|
+
// Server - receive image
|
|
508
508
|
stelar.on('image', (ctx) => {
|
|
509
|
-
// ctx.buffer
|
|
510
|
-
console.log('
|
|
511
|
-
//
|
|
509
|
+
// ctx.buffer is a Uint8Array
|
|
510
|
+
console.log('Received:', ctx.buffer.byteLength, 'bytes');
|
|
511
|
+
// Save or process the image
|
|
512
512
|
saveImage(ctx.buffer);
|
|
513
513
|
|
|
514
|
-
//
|
|
514
|
+
// Respond to client
|
|
515
515
|
ctx.emit('imageSaved', { success: true });
|
|
516
516
|
});
|
|
517
517
|
|
|
518
|
-
//
|
|
518
|
+
// Client - send image
|
|
519
519
|
const input = document.querySelector('input[type="file"]');
|
|
520
520
|
input.addEventListener('change', async (e) => {
|
|
521
521
|
const file = e.target.files[0];
|
|
@@ -523,7 +523,7 @@ input.addEventListener('change', async (e) => {
|
|
|
523
523
|
client.emitBinary('image', buffer);
|
|
524
524
|
});
|
|
525
525
|
|
|
526
|
-
//
|
|
526
|
+
// Client - receive image
|
|
527
527
|
client.on('image', (buffer) => {
|
|
528
528
|
const blob = new Blob([buffer], { type: 'image/png' });
|
|
529
529
|
const url = URL.createObjectURL(blob);
|
|
@@ -531,35 +531,35 @@ client.on('image', (buffer) => {
|
|
|
531
531
|
});
|
|
532
532
|
```
|
|
533
533
|
|
|
534
|
-
### Broadcast
|
|
534
|
+
### Binary Broadcast
|
|
535
535
|
|
|
536
536
|
```javascript
|
|
537
|
-
//
|
|
537
|
+
// Server - share file with everyone
|
|
538
538
|
stelar.on('upload', (ctx) => {
|
|
539
539
|
ctx.broadcastBinary('file', ctx.buffer);
|
|
540
540
|
});
|
|
541
541
|
|
|
542
|
-
//
|
|
542
|
+
// Client - send file
|
|
543
543
|
const fileData = await file.arrayBuffer();
|
|
544
544
|
client.emitBinary('upload', fileData);
|
|
545
545
|
```
|
|
546
546
|
|
|
547
547
|
---
|
|
548
548
|
|
|
549
|
-
##
|
|
549
|
+
## Difference with Socket.io
|
|
550
550
|
|
|
551
|
-
|
|
|
552
|
-
|
|
553
|
-
|
|
|
554
|
-
|
|
|
555
|
-
|
|
|
556
|
-
|
|
|
557
|
-
| Ideal
|
|
551
|
+
| Feature | stelar-time-real | Socket.io |
|
|
552
|
+
|---------|------------------|-----------|
|
|
553
|
+
| Heap size | ~13 MB | ~50-100 MB |
|
|
554
|
+
| Dependencies | ws (1) | multiple |
|
|
555
|
+
| Configuration | minimal | complex |
|
|
556
|
+
| Flexibility | total | opinionated |
|
|
557
|
+
| Ideal for | own projects | quick production |
|
|
558
558
|
|
|
559
|
-
##
|
|
559
|
+
## License
|
|
560
560
|
|
|
561
561
|
MIT - Stelar
|
|
562
562
|
|
|
563
|
-
##
|
|
563
|
+
## Author
|
|
564
564
|
|
|
565
565
|
Stelar
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stelar-time-real",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"description": "WebSocket liviano y rápido sin dependendas en TypeScript. Alternativa a socket.io, namespaces, ACKs y archivos binarios.",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
@@ -22,12 +22,12 @@
|
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
25
|
-
"url": "https://github.com/
|
|
25
|
+
"url": "https://github.com/foxytp/stelar-time-real"
|
|
26
26
|
},
|
|
27
27
|
"bugs": {
|
|
28
|
-
"url": "https://github.com/
|
|
28
|
+
"url": "https://github.com/foxytp/stelar-time-real/issues"
|
|
29
29
|
},
|
|
30
|
-
"homepage": "https://github.com/
|
|
30
|
+
"homepage": "https://github.com/foxytp/stelar-time-real#readme",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": "./src/index.js",
|
|
33
33
|
"./client": "./src/client.js",
|