relayx-js 1.0.19 → 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.
@@ -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
- # Relay NodeJS Library
2
- ![License](https://img.shields.io/badge/Apache_2.0-green?label=License)<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
- ## Features
6
- 1. Real-time communication made easy—connect, publish, and subscribe with minimal effort.
7
- 2. Automatic reconnection built-in, with a 2-minute retry window for network disruptions.
8
- 3. Message persistence during reconnection ensures no data loss when the client reconnects.
3
+ ![License](https://img.shields.io/badge/Apache_2.0-green?label=License)
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
- Install the relay library by running the command below in your terminal<br>
12
- `npm install relayx-js`
13
-
14
- ## Usage
15
- ### Prerequisites
16
- 1. Obtain API key and Secret key
17
- 2. Initialize the library
18
- ```javascript
19
- import { Realtime, CONNECTED, RECONNECT, DISCONNECTED } from "relayx-js"
20
-
21
- var realtime = new Realtime({
22
- api_key: process.env.api_key,
23
- secret: process.env.secret,
24
- });
25
- realtime.init();
26
-
27
- // Initialization of topic listeners go here... (look at examples/example_chat.js for full implementation)
28
-
29
- realtime.connect();
30
-
31
- // Other application logic...
32
- ```
33
-
34
- ### Usage
35
- 1. <b>Publish</b><br>
36
- Send a message to a topic:<br>
37
- ```javascript
38
- var sent = await realtime.publish("power_telemetry", {
39
- "voltage_V": 5,
40
- "current_mA": 400,
41
- "power_W": 2
42
- });
43
-
44
- if(sent){
45
- console.log("Message was successfully sent to topic => power_telemetry");
46
- }else{
47
- console.log("Message was not sent to topic => power_telemetry");
48
- }
49
- ```
50
- 2. <b>Listen</b><br>
51
- Subscribe to a topic to receive messages:<br>
52
- ```javascript
53
- await realtime.on("power_telemetry", (data) => {
54
- console.log(data);
55
- });
56
- ```
57
- 3. <b>Turn Off Listener</b><br>
58
- Unsubscribe from a topic:<br>
59
- ```javascript
60
- var unsubscribed = await realtime.off("power_telemetry");
61
-
62
- if(unsubscribed){
63
- console.log("Successfully unsubscribed from power_telemetry");
64
- }else{
65
- console.log("Unable to unsubscribe from power_telemetry");
66
- }
67
- ```
68
- 4. <b>History</b><br>
69
- Get previously published messages between a start date and end date. Dates are in UTC.
70
- ```javascript
71
- var start = new Date();
72
- var past = start.setDate(start.getDate() - 4) // Get start date from 4 days ago
73
- var startDate = new Date(past)
74
-
75
- var end = new Date();
76
- var past = end.setDate(end.getDate() - 2) // Get end date from 2 days ago
77
- var endDate = new Date(past)
78
-
79
- var history = await realtime.history(topic, startDate, endDate)
80
- ```
81
- The end date is optional. Supplying only the start time will fetch all messages from the start time to now.
82
- ```javascript
83
- var start = new Date();
84
- var past = start.setDate(start.getDate() - 4) // Get start date from 4 days ago
85
- var startDate = new Date(past)
86
-
87
- // This will get all messages from 4 days ago to now
88
- var history = await realtime.history(topic, startDate)
89
- ```
90
- 5. <b>Valid Topic Check</b><br>
91
- Utility function to check if a particular topic is valid
92
- ```javascript
93
- var isValid = realtime.isTopicValid("topic");
94
-
95
- console.log(`Topic Valid => ${isValid}`);
96
- ```
97
- 6. <b>Sleep</b><br>
98
- Utility async function to delay code execution
99
- ```javascript
100
- console.log("Starting code execution...");
101
- await realtime.sleep(2000) // arg is in ms
102
- console.log("This line executed after 2 seconds");
103
- ```
104
- 7. <b>Close Connection to Relay</b><br>
105
- Manually disconnect from the Relay Network
106
- ```javascript
107
- // Logic here
108
-
109
- realtime.close();
110
- ```
111
-
112
- ## System Events
113
- 1. <b>CONNECTED</b><br>
114
- This event is fired when the library connects to the Relay Network.
115
- ```javascript
116
- await realtime.on(CONNECTED, () => {
117
- console.log("Connected to the Relay Network!");
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.
@@ -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(false, {
15
- max_retries: 2,
16
- debug: true
16
+ await realtime.init({
17
+ staging: true,
18
+ opts: {
19
+ max_retries: 2,
20
+ debug: false
21
+ }
17
22
  });
18
23
 
19
- realtime.on(CONNECTED, async () => {
20
- console.log("[IMPL] => CONNECTED!");
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] DISONNECT`)
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() - 4)
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 realtime.publish(topic, input);
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(true, {
9
- max_retries: 2,
10
- debug: true
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.19",
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.28.2",
26
+ "nats": "2.29.3",
24
27
  "uuid": "11.1.0"
25
28
  },
26
29
  "devDependencies": {