relayx-js 1.0.18 → 1.1.0
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/.claude/settings.local.json +19 -0
- package/LICENSE +9 -0
- package/README.md +121 -183
- package/examples/example_chat.js +95 -9
- package/examples/example_send_data_on_connect.js +6 -3
- package/package.json +6 -3
- package/realtime/kv_storage.js +196 -0
- package/realtime/models/message.js +26 -0
- package/realtime/queue.js +653 -0
- package/realtime/realtime.js +196 -143
- package/realtime/utils.js +114 -0
- package/tests/test_kv.js +679 -0
- package/tests/test_queue.js +568 -0
- package/tests/test.js +0 -685
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"permissions": {
|
|
3
|
+
"allow": [
|
|
4
|
+
"Bash(node tests/test_queue.js:*)",
|
|
5
|
+
"Bash(npm test:*)",
|
|
6
|
+
"Bash(node /tmp/debug_pattern.js:*)",
|
|
7
|
+
"Bash(node /tmp/trace_pattern.js:*)",
|
|
8
|
+
"Bash(node /tmp/debug_pattern_fixed.js:*)",
|
|
9
|
+
"Bash(ENV=test node:*)",
|
|
10
|
+
"Bash(npm run test:kv:*)",
|
|
11
|
+
"Bash(set -o allexport)",
|
|
12
|
+
"Bash(source .env)",
|
|
13
|
+
"Bash(set +o allexport)",
|
|
14
|
+
"Bash(NODE_ENV=test node:*)"
|
|
15
|
+
],
|
|
16
|
+
"deny": [],
|
|
17
|
+
"ask": []
|
|
18
|
+
}
|
|
19
|
+
}
|
package/LICENSE
CHANGED
|
@@ -174,6 +174,15 @@
|
|
|
174
174
|
of your accepting any such warranty or additional liability.
|
|
175
175
|
|
|
176
176
|
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2025 Terraorbital Networks (OPC) Pvt Ltd
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
177
186
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
178
187
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
179
188
|
See the License for the specific language governing permissions and
|
package/README.md
CHANGED
|
@@ -1,186 +1,124 @@
|
|
|
1
|
-
#
|
|
2
|
-
<br>
|
|
3
|
-
A powerful library for integrating real-time communication into your software stack, powered by the Relay Network.
|
|
1
|
+
# RelayX JavaScript SDK
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Real-time messaging, queues, and key-value storage for JavaScript applications.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## What is RelayX?
|
|
10
|
+
|
|
11
|
+
RelayX is a real-time communication platform that enables pub/sub messaging, distributed queues, and key-value storage. This SDK provides a simple JavaScript interface to build real-time applications with minimal setup.
|
|
12
|
+
|
|
13
|
+
---
|
|
9
14
|
|
|
10
15
|
## Installation
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
2. <b>RECONNECT</b><br>
|
|
122
|
-
This event is fired when the library reconnects to the Relay Network. This is only fired when the disconnection event is not manual, i.e, disconnection due to network issues.
|
|
123
|
-
```javascript
|
|
124
|
-
await realtime.on(RECONNECT, (status) => {
|
|
125
|
-
console.log(`Reconnected! => ${status}`);
|
|
126
|
-
});
|
|
127
|
-
```
|
|
128
|
-
`status` can have values of `RECONNECTING` & `RECONNECTED`.
|
|
129
|
-
|
|
130
|
-
`RECONNECTING` => Reconnection attempts have begun. If `status == RECONNECTING`, the `RECONNECT` event is fired every 1 second.<br>
|
|
131
|
-
`RECONNECTED` => Reconnected to the Relay Network.
|
|
132
|
-
3. <b>DISCONNECTED</b><br>
|
|
133
|
-
This event is fired when the library disconnects from the Relay Network. This includes disconnection due to network issues as well.
|
|
134
|
-
```javascript
|
|
135
|
-
await realtime.on(DISCONNECTED, () => {
|
|
136
|
-
console.log("Disconnected from the Relay Network");
|
|
137
|
-
});
|
|
138
|
-
```
|
|
139
|
-
4. <b>MESSAGE_RESEND</b><br>
|
|
140
|
-
This event is fired when the library resends the messages upon reconnection to the Relay Network.
|
|
141
|
-
```javascript
|
|
142
|
-
await realtime.on(MESSAGE_RESEND, (messages) => {
|
|
143
|
-
console.log("Offline messages may have been resent");
|
|
144
|
-
console.log("Messages");
|
|
145
|
-
console.log(messages);
|
|
146
|
-
});
|
|
147
|
-
```
|
|
148
|
-
`messages` is an array of the following object,<br>
|
|
149
|
-
```json
|
|
150
|
-
{
|
|
151
|
-
"topic": "<topic the message belongs to>",
|
|
152
|
-
"message": "<message you sent>",
|
|
153
|
-
"resent": "<boolean, indicating if the message was sent successully>"
|
|
154
|
-
}
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
## API Reference
|
|
158
|
-
1. init()<br>
|
|
159
|
-
Initializes library with configuration options.
|
|
160
|
-
* debug (boolean): enables library level logging
|
|
161
|
-
2. connect()<br>
|
|
162
|
-
Connects the library to the Relay Network. This is an async function.
|
|
163
|
-
3. close()<br>
|
|
164
|
-
Disconnects the library from the Relay Network.
|
|
165
|
-
3. on()<br>
|
|
166
|
-
Subscribes to a topic. This is an async function.
|
|
167
|
-
* @param {string} topic - Name of the event
|
|
168
|
-
* @param {function} func - Callback function to call on user thread
|
|
169
|
-
* @returns {boolean} - To check if topic subscription was successful
|
|
170
|
-
3. off()<br>
|
|
171
|
-
Deletes reference to user defined event callback for a topic. This will stop listening to a topic. This is an async function.
|
|
172
|
-
* @param {string} topic
|
|
173
|
-
* @returns {boolean} - To check if topic unsubscribe was successful
|
|
174
|
-
4. history()<br>
|
|
175
|
-
Get a list of messages published in the past. This is an async function.<br>
|
|
176
|
-
A list of messages can be obtained using a start time and end time. End time is optional. If end time is not specified, all messages from the start time to now is returned.
|
|
177
|
-
* @param {string} topic
|
|
178
|
-
* @param {Date} start
|
|
179
|
-
* @param {Date} end
|
|
180
|
-
* @returns {JSON Array} - List of messages published in the past
|
|
181
|
-
5. isTopicValid()<br>
|
|
182
|
-
Checks if a topic can be used to send messages to.
|
|
183
|
-
* @param {string} topic - Name of event
|
|
184
|
-
* @returns {boolean} - If topic is valid or not
|
|
185
|
-
6. sleep()<br>
|
|
186
|
-
Pauses code execution for a user defined time. Time passed into the method is in milliseconds. This is an async function.
|
|
16
|
+
|
|
17
|
+
Install via npm:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install relayx-js
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
import { Realtime } from 'relayx-js';
|
|
29
|
+
|
|
30
|
+
const client = new Realtime({
|
|
31
|
+
api_key: 'your-api-key',
|
|
32
|
+
secret: 'your-secret'
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
await client.init();
|
|
36
|
+
await client.connect();
|
|
37
|
+
|
|
38
|
+
await client.on('messages', (msg) => {
|
|
39
|
+
console.log('Received:', msg.data);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await client.publish('messages', { text: 'Hello RelayX!' });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Messaging (Pub/Sub)
|
|
48
|
+
|
|
49
|
+
**Publishing Messages:**
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
await client.publish('chat.room1', {
|
|
53
|
+
user: 'alice',
|
|
54
|
+
message: 'Hello world'
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Subscribing to Messages:**
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
await client.on('chat.room1', (msg) => {
|
|
62
|
+
console.log(`${msg.data.user}: ${msg.data.message}`);
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
---
|
|
67
|
+
|
|
68
|
+
## Queues
|
|
69
|
+
|
|
70
|
+
**Publishing a Job:**
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
const queue = await client.initQueue('tasks');
|
|
74
|
+
|
|
75
|
+
await queue.publish('email.send', {
|
|
76
|
+
to: 'user@example.com',
|
|
77
|
+
subject: 'Welcome'
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Consuming Jobs:**
|
|
82
|
+
|
|
83
|
+
```javascript
|
|
84
|
+
await queue.consume({ topic: 'email.send' }, async (job) => {
|
|
85
|
+
console.log('Processing:', job.data);
|
|
86
|
+
job.ack();
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Key-Value Store
|
|
93
|
+
|
|
94
|
+
**Storing Data:**
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
const kv = await client.initKVStore();
|
|
98
|
+
|
|
99
|
+
await kv.put('user.123', {
|
|
100
|
+
name: 'Alice',
|
|
101
|
+
status: 'active'
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Retrieving Data:**
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
const user = await kv.get('user.123');
|
|
109
|
+
console.log(user.name);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Documentation
|
|
115
|
+
|
|
116
|
+
For complete documentation including guarantees, limits, API reference, and advanced usage:
|
|
117
|
+
|
|
118
|
+
**https://docs.relay-x.io**
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## License
|
|
123
|
+
|
|
124
|
+
This SDK is licensed under the Apache 2.0 License.
|
package/examples/example_chat.js
CHANGED
|
@@ -6,18 +6,99 @@ const rl = readline.createInterface({
|
|
|
6
6
|
output: process.stdout
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
+
var queue = null;
|
|
10
|
+
|
|
9
11
|
async function run(){
|
|
10
12
|
var realtime = new Realtime({
|
|
11
13
|
api_key: process.env.AUTH_JWT,
|
|
12
14
|
secret: process.env.AUTH_SECRET
|
|
13
15
|
});
|
|
14
|
-
await realtime.init(
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
await realtime.init({
|
|
17
|
+
staging: true,
|
|
18
|
+
opts: {
|
|
19
|
+
max_retries: 2,
|
|
20
|
+
debug: false
|
|
21
|
+
}
|
|
17
22
|
});
|
|
18
23
|
|
|
19
|
-
realtime.
|
|
20
|
-
|
|
24
|
+
queue = await realtime.initQueue("692adca3af5ed9d55e1b1ece");
|
|
25
|
+
|
|
26
|
+
realtime.on(CONNECTED, async (connected) => {
|
|
27
|
+
console.log(`[IMPL] => ${connected ? 'CONNECTED!' : "AUTH FAILURE"}`);
|
|
28
|
+
|
|
29
|
+
if(!connected){
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var kvStore = await realtime.initKVStore();
|
|
34
|
+
console.log(`KV Store => ${kvStore}`)
|
|
35
|
+
|
|
36
|
+
// console.log("Put()")
|
|
37
|
+
// await kvStore.put("key1", {
|
|
38
|
+
// hey: "World!"
|
|
39
|
+
// })
|
|
40
|
+
|
|
41
|
+
// await kvStore.put("key2", [{
|
|
42
|
+
// hey: "World!"
|
|
43
|
+
// }])
|
|
44
|
+
|
|
45
|
+
// await kvStore.put("key3", [123, 123, 123])
|
|
46
|
+
|
|
47
|
+
await kvStore.put("key4", "")
|
|
48
|
+
|
|
49
|
+
// await kvStore.put("key5", ["hey", "heyb"])
|
|
50
|
+
|
|
51
|
+
// await kvStore.put("key6", 123)
|
|
52
|
+
|
|
53
|
+
// await kvStore.put("key6", null)
|
|
54
|
+
|
|
55
|
+
// await kvStore.put("key7", undefined)
|
|
56
|
+
|
|
57
|
+
// await kvStore.put("key8", true)
|
|
58
|
+
|
|
59
|
+
// await kvStore.put("key9", false)
|
|
60
|
+
|
|
61
|
+
// await kvStore.put("key10", 10.123)
|
|
62
|
+
|
|
63
|
+
console.log()
|
|
64
|
+
console.log("keys()")
|
|
65
|
+
var keys = await kvStore.keys()
|
|
66
|
+
console.log(keys)
|
|
67
|
+
|
|
68
|
+
for(const k of keys){
|
|
69
|
+
console.log()
|
|
70
|
+
console.log(`get(${k})`)
|
|
71
|
+
console.log(await kvStore.get(k))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// for(const k of keys){
|
|
75
|
+
// console.log()
|
|
76
|
+
// console.log(`delete(${k})`)
|
|
77
|
+
// await kvStore.delete(k)
|
|
78
|
+
// }
|
|
79
|
+
|
|
80
|
+
// console.log()
|
|
81
|
+
// console.log("keys()")
|
|
82
|
+
// var keys = await kvStore.keys()
|
|
83
|
+
// console.log(keys)
|
|
84
|
+
|
|
85
|
+
queue = await realtime.initQueue("692adca3af5ed9d55e1b1ece");
|
|
86
|
+
|
|
87
|
+
var config = {
|
|
88
|
+
name: "Test434",
|
|
89
|
+
group: "test-group",
|
|
90
|
+
topic: "queue.>",
|
|
91
|
+
// backoff: [2, 5, 10],
|
|
92
|
+
"ack_wait": 2
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var count = 0;
|
|
96
|
+
|
|
97
|
+
queue.consume(config, (msg) => {
|
|
98
|
+
console.log(`Queue: ${msg.message}`)
|
|
99
|
+
|
|
100
|
+
msg.ack();
|
|
101
|
+
})
|
|
21
102
|
});
|
|
22
103
|
|
|
23
104
|
realtime.on(RECONNECT, (status) => {
|
|
@@ -25,7 +106,7 @@ async function run(){
|
|
|
25
106
|
});
|
|
26
107
|
|
|
27
108
|
realtime.on(DISCONNECTED, () => {
|
|
28
|
-
console.log(`[IMPL]
|
|
109
|
+
console.log(`[IMPL] DISCONNECT`)
|
|
29
110
|
});
|
|
30
111
|
|
|
31
112
|
await realtime.on("power-telemetry", (data) => {
|
|
@@ -42,7 +123,7 @@ async function run(){
|
|
|
42
123
|
|
|
43
124
|
// await realtime.on("hello.hey.*", (data) => {
|
|
44
125
|
// console.log("hell.hey.*", data);
|
|
45
|
-
// })
|
|
126
|
+
// });`
|
|
46
127
|
|
|
47
128
|
// await realtime.on("hello.hey.>", (data) => {
|
|
48
129
|
// console.log("hello.hey.>", data);
|
|
@@ -65,7 +146,7 @@ async function run(){
|
|
|
65
146
|
}else if(input == "history"){
|
|
66
147
|
rl.question("topic: ", async (topic) => {
|
|
67
148
|
var start = new Date();
|
|
68
|
-
var past = start.setDate(start.getDate() -
|
|
149
|
+
var past = start.setDate(start.getDate() - 1)
|
|
69
150
|
var pastDate = new Date(past)
|
|
70
151
|
|
|
71
152
|
var end = new Date();
|
|
@@ -93,9 +174,14 @@ async function run(){
|
|
|
93
174
|
console.log(topic, data);
|
|
94
175
|
});
|
|
95
176
|
})
|
|
177
|
+
}else if(input == "delete_consumer"){
|
|
178
|
+
rl.question("Consumer name: ", async (name) => {
|
|
179
|
+
var del = await queue.deleteConsumer(name);
|
|
180
|
+
console.log(del)
|
|
181
|
+
})
|
|
96
182
|
}else{
|
|
97
183
|
rl.question("topic: ", async (topic) => {
|
|
98
|
-
var output = await
|
|
184
|
+
var output = await queue.publish(topic, input);
|
|
99
185
|
})
|
|
100
186
|
}
|
|
101
187
|
});
|
|
@@ -5,9 +5,12 @@ async function run(){
|
|
|
5
5
|
api_key: process.env.AUTH_JWT,
|
|
6
6
|
secret: process.env.AUTH_SECRET
|
|
7
7
|
});
|
|
8
|
-
await realtime.init(
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
await realtime.init({
|
|
9
|
+
staging: true,
|
|
10
|
+
opts: {
|
|
11
|
+
max_retries: 2,
|
|
12
|
+
debug: true
|
|
13
|
+
}
|
|
11
14
|
});
|
|
12
15
|
|
|
13
16
|
realtime.on(CONNECTED, async () => {
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "relayx-js",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"main": "realtime/realtime.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "NODE_ENV=test node tests/test.js"
|
|
7
|
+
"test": "set -o allexport && source .env && set +o allexport && NODE_ENV=test node tests/test.js",
|
|
8
|
+
"test:queue": "set -o allexport && source .env && set +o allexport && NODE_ENV=test node tests/test_queue.js",
|
|
9
|
+
"test:kv": "set -o allexport && source .env && set +o allexport && NODE_ENV=test node tests/test_kv.js"
|
|
8
10
|
},
|
|
9
11
|
"keywords": [
|
|
10
12
|
"realtime",
|
|
@@ -19,8 +21,9 @@
|
|
|
19
21
|
"dependencies": {
|
|
20
22
|
"@msgpack/msgpack": "3.1.1",
|
|
21
23
|
"@nats-io/jetstream": "3.0.0-35",
|
|
24
|
+
"@nats-io/kv": "3.2.0",
|
|
22
25
|
"jest": "29.7.0",
|
|
23
|
-
"nats": "2.
|
|
26
|
+
"nats": "2.29.3",
|
|
24
27
|
"uuid": "11.1.0"
|
|
25
28
|
},
|
|
26
29
|
"devDependencies": {
|