@motiadev/stream-client-node 0.5.2-beta.104-330172
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/LICENSE +21 -0
- package/README.md +185 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/src/stream-adapter.d.ts +13 -0
- package/dist/src/stream-adapter.js +28 -0
- package/dist/src/stream.d.ts +4 -0
- package/dist/src/stream.js +7 -0
- package/index.ts +2 -0
- package/package.json +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Motia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# @motiadev/stream-client-node
|
|
2
|
+
|
|
3
|
+
Motia Stream Client Package – Responsible for managing real-time [Motia](https://motia.dev) streams of data in node environments. This package provides a simple, type-safe interface for subscribing to item and group streams over WebSockets, handling live updates, and managing stream state in your web applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **WebSocket-based streaming** for real-time data updates
|
|
8
|
+
- **Item and group subscriptions** with automatic state management
|
|
9
|
+
- **Change listeners** for reactive UI updates
|
|
10
|
+
- **Custom event handling** for extensibility
|
|
11
|
+
- **TypeScript support** for strong typing and autocompletion
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @motiadev/stream-client-node
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### 1. Creating a Stream Connection
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Stream } from '@motiadev/stream-client-node'
|
|
29
|
+
|
|
30
|
+
const stream = new Stream('wss://your-stream-server', () => {
|
|
31
|
+
console.log('WebSocket connection established!')
|
|
32
|
+
})
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### 2. Subscribing to an Item Stream
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
const itemSubscription = stream.subscribeItem<{ id: string; name: string }>('users', 'user-123')
|
|
39
|
+
|
|
40
|
+
itemSubscription.addChangeListener((user) => {
|
|
41
|
+
console.log('User updated:', user)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
// Listen for custom events
|
|
45
|
+
itemSubscription.onEvent('custom-event', (data) => {
|
|
46
|
+
console.log('Received custom event:', data)
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### 3. Subscribing to a Group Stream
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
const groupSubscription = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-abc')
|
|
54
|
+
|
|
55
|
+
groupSubscription.addChangeListener((users) => {
|
|
56
|
+
console.log('Group users updated:', users)
|
|
57
|
+
})
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 4. Cleaning Up
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
itemSubscription.close()
|
|
64
|
+
groupSubscription.close()
|
|
65
|
+
stream.close()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## API Reference
|
|
71
|
+
|
|
72
|
+
### `Stream`
|
|
73
|
+
|
|
74
|
+
- **constructor(address: string, onReady: () => void)**
|
|
75
|
+
|
|
76
|
+
- Establishes a WebSocket connection to the given address.
|
|
77
|
+
- Calls `onReady` when the connection is open.
|
|
78
|
+
|
|
79
|
+
- **subscribeItem<T>(streamName: string, id: string): StreamItemSubscription<T>**
|
|
80
|
+
|
|
81
|
+
- Subscribes to a single item in a stream.
|
|
82
|
+
- Returns a `StreamItemSubscription` instance.
|
|
83
|
+
|
|
84
|
+
- **subscribeGroup<T>(streamName: string, groupId: string): StreamGroupSubscription<T>**
|
|
85
|
+
|
|
86
|
+
- Subscribes to a group of items in a stream.
|
|
87
|
+
- Returns a `StreamGroupSubscription` instance.
|
|
88
|
+
|
|
89
|
+
- **close()**
|
|
90
|
+
- Closes the WebSocket connection.
|
|
91
|
+
|
|
92
|
+
---
|
|
93
|
+
|
|
94
|
+
### `StreamItemSubscription<T>`
|
|
95
|
+
|
|
96
|
+
- **addChangeListener(listener: (item: T | null) => void)**
|
|
97
|
+
|
|
98
|
+
- Registers a callback for when the item changes.
|
|
99
|
+
|
|
100
|
+
- **removeChangeListener(listener)**
|
|
101
|
+
|
|
102
|
+
- Unregisters a change listener.
|
|
103
|
+
|
|
104
|
+
- **onEvent(type: string, listener: (event: any) => void)**
|
|
105
|
+
|
|
106
|
+
- Registers a custom event listener.
|
|
107
|
+
|
|
108
|
+
- **offEvent(type: string, listener)**
|
|
109
|
+
|
|
110
|
+
- Unregisters a custom event listener.
|
|
111
|
+
|
|
112
|
+
- **getState(): T | null**
|
|
113
|
+
|
|
114
|
+
- Returns the current state of the item.
|
|
115
|
+
|
|
116
|
+
- **close()**
|
|
117
|
+
- Unsubscribes from the item stream and cleans up listeners.
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
### `StreamGroupSubscription<T>`
|
|
122
|
+
|
|
123
|
+
- **addChangeListener(listener: (items: T[]) => void)**
|
|
124
|
+
|
|
125
|
+
- Registers a callback for when the group changes.
|
|
126
|
+
|
|
127
|
+
- **removeChangeListener(listener)**
|
|
128
|
+
|
|
129
|
+
- Unregisters a change listener.
|
|
130
|
+
|
|
131
|
+
- **onEvent(type: string, listener: (event: any) => void)**
|
|
132
|
+
|
|
133
|
+
- Registers a custom event listener.
|
|
134
|
+
|
|
135
|
+
- **offEvent(type: string, listener: (event: any) => void)**
|
|
136
|
+
|
|
137
|
+
- Unregisters a custom event listener.
|
|
138
|
+
|
|
139
|
+
- **getState(): T[]**
|
|
140
|
+
|
|
141
|
+
- Returns the current state of the group.
|
|
142
|
+
|
|
143
|
+
- **close()**
|
|
144
|
+
- Unsubscribes from the group stream and cleans up listeners.
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
### `StreamSubscription` (Base Class)
|
|
149
|
+
|
|
150
|
+
- **onEvent(type: string, listener: (event: any) => void)**
|
|
151
|
+
- **offEvent(type: string, listener: (event: any) => void)**
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Types
|
|
156
|
+
|
|
157
|
+
All types are exported from `stream.types.ts` for advanced usage and type safety.
|
|
158
|
+
|
|
159
|
+
---
|
|
160
|
+
|
|
161
|
+
## Example
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
import { Stream } from '@motiadev/stream-client-node'
|
|
165
|
+
|
|
166
|
+
const stream = new Stream('wss://example.com', () => {
|
|
167
|
+
const userSub = stream.subscribeItem<{ id: string; name: string }>('users', 'user-1')
|
|
168
|
+
|
|
169
|
+
userSub.addChangeListener((user) => {
|
|
170
|
+
// React to user changes
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const groupSub = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-1')
|
|
174
|
+
|
|
175
|
+
groupSub.addChangeListener((users) => {
|
|
176
|
+
// React to group changes
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
## License
|
|
184
|
+
|
|
185
|
+
MIT
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { SocketAdapter } from '@motiadev/stream-client';
|
|
2
|
+
export declare class StreamSocketAdapter implements SocketAdapter {
|
|
3
|
+
private address;
|
|
4
|
+
private ws;
|
|
5
|
+
constructor(address: string);
|
|
6
|
+
connect(): void;
|
|
7
|
+
send(message: string): void;
|
|
8
|
+
onMessage(callback: (message: string) => void): void;
|
|
9
|
+
onOpen(callback: () => void): void;
|
|
10
|
+
onClose(callback: () => void): void;
|
|
11
|
+
close(): void;
|
|
12
|
+
isOpen(): boolean;
|
|
13
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { WebSocket } from 'ws';
|
|
2
|
+
export class StreamSocketAdapter {
|
|
3
|
+
constructor(address) {
|
|
4
|
+
this.address = address;
|
|
5
|
+
this.ws = new WebSocket(this.address);
|
|
6
|
+
}
|
|
7
|
+
connect() { }
|
|
8
|
+
send(message) {
|
|
9
|
+
this.ws.send(message);
|
|
10
|
+
}
|
|
11
|
+
onMessage(callback) {
|
|
12
|
+
const listener = (message) => callback(message.data.toString());
|
|
13
|
+
this.ws.addEventListener('message', listener);
|
|
14
|
+
}
|
|
15
|
+
onOpen(callback) {
|
|
16
|
+
this.ws.addEventListener('open', callback);
|
|
17
|
+
}
|
|
18
|
+
onClose(callback) {
|
|
19
|
+
this.ws.addEventListener('close', callback);
|
|
20
|
+
}
|
|
21
|
+
close() {
|
|
22
|
+
this.ws.close();
|
|
23
|
+
this.ws.removeAllListeners();
|
|
24
|
+
}
|
|
25
|
+
isOpen() {
|
|
26
|
+
return this.ws.readyState === WebSocket.OPEN;
|
|
27
|
+
}
|
|
28
|
+
}
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@motiadev/stream-client-node",
|
|
3
|
+
"description": "Motia Stream Client Package – Responsible for managing streams of data.",
|
|
4
|
+
"version": "0.5.2-beta.104-330172",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"uuid": "^11.1.0",
|
|
10
|
+
"ws": "^8.18.2",
|
|
11
|
+
"@motiadev/stream-client": "0.5.2-beta.104-330172"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/jest": "^29.5.14",
|
|
15
|
+
"@types/ws": "^8.18.1",
|
|
16
|
+
"jest": "^29.7.0",
|
|
17
|
+
"ts-jest": "^29.3.2",
|
|
18
|
+
"typescript": "^5.7.2"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rm -rf dist && tsc",
|
|
22
|
+
"lint": "eslint --config ../../eslint.config.js"
|
|
23
|
+
}
|
|
24
|
+
}
|