relayx-js 1.0.7 → 1.0.9

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/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ V1.0.8
2
+ - History API fetch() to consume()
3
+
1
4
  V1.0.7
2
5
  - History API added
3
6
  - README updated to explain history API usage
package/README.md CHANGED
@@ -66,7 +66,7 @@ Unsubscribe from a topic:<br>
66
66
  }
67
67
  ```
68
68
  4. <b>History</b><br>
69
- Get previously published messages between a start date and end date
69
+ Get previously published messages between a start date and end date. Dates are in UTC.
70
70
  ```javascript
71
71
  var start = new Date();
72
72
  var past = start.setDate(start.getDate() - 4) // Get start date from 4 days ago
@@ -8,8 +8,8 @@ const rl = readline.createInterface({
8
8
 
9
9
  async function run(){
10
10
  var realtime = new Realtime({
11
- api_key: process.env.user_key,
12
- secret: process.env.secret
11
+ api_key: "eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5LW5rZXkifQ.eyJhdWQiOiJOQVRTIiwibmFtZSI6IjY3ZmZhMWVkMzk0NDRlODMwOGIxMDBiNCIsInN1YiI6IlVENUtRN0hNU01IMlNVWklCTTNCU1IyMjdQNUw3NFZKVTNUS0tPMk5XT1U2Qkg2S0pEV0JCNEFNIiwibmF0cyI6eyJkYXRhIjotMSwicGF5bG9hZCI6LTEsInN1YnMiOi0xLCJwdWIiOnsiZGVueSI6WyI-Il19LCJzdWIiOnsiZGVueSI6WyI-Il19LCJvcmdfZGF0YSI6eyJvcmdfaWQiOiI2N2ZmYTFkMzRmMDQ5MTAzNGQ5ZWJiNzIiLCJvcmdfbmFtZSI6IlNwYWNlWCIsInZhbGlkaXR5X2tleSI6IjU3NjUyM2UwLWM3ZGMtNGVlMS04OTFjLTAzNTFmMDgxOTkzYyIsInJvbGUiOiJ1c2VyIiwicHJvamVjdF9pZCI6IjY3ZmZhMWVkMzk0NDRlODMwOGIxMDBiNCJ9LCJpc3N1ZXJfYWNjb3VudCI6IkFCMzczR0JSWkVMNEhLNUdXWkJNNFNVTEFLVUhZSTVNUE9UWVNKQUpCVVVNU01KT09XREhFMjVYIiwidHlwZSI6InVzZXIiLCJ2ZXJzaW9uIjoyfSwiaXNzIjoiQUIzNzNHQlJaRUw0SEs1R1daQk00U1VMQUtVSFlJNU1QT1RZU0pBSkJVVU1TTUpPT1dESEUyNVgiLCJpYXQiOjE3NDQ4MDYzOTQsImp0aSI6Im5ZczM4MEZxa1dnNis3eGl4Wnl6VjJWS0ZLSVFNOEJrOG9ESFFzTjdBcW1YaUNmd05HTnNkQnVNK21YMW8xN0dFM2YxeEJXSFFGU1VUSmFrTmN1MGNnPT0ifQ.FVRHylWkCyIN0Axed0Z-H2Zgfh3HjEHGkjWQfUe70O3hl3OQYkQa1Ka-IYilGPB2fpd272t3VJarMvF1e5OSCw",
12
+ secret: "SUAC7MP5AIOVJJTV2DCKRQRVAANBETTVYP26O4YE2BXELFOCURXLMEX5YE"
13
13
  });
14
14
  await realtime.init(true, {
15
15
  max_retries: 2,
@@ -28,8 +28,8 @@ async function run(){
28
28
  console.log(`[IMPL] DISONNECT`)
29
29
  });
30
30
 
31
- await realtime.on("hello", (data) => {
32
- console.log("hello", data);
31
+ await realtime.on("power-telemetry", (data) => {
32
+ console.log("power-telemetry", data);
33
33
  });
34
34
 
35
35
  await realtime.on("hello1", (data) => {
@@ -61,7 +61,7 @@ async function run(){
61
61
  var endDate = new Date(past)
62
62
 
63
63
  var history = await realtime.history(topic, pastDate, endDate)
64
- console.log(history)
64
+ // console.log(history)
65
65
  })
66
66
  }else if(input == "off"){
67
67
  rl.question("topic to off(): ", async (topic) => {
@@ -2,12 +2,12 @@ import { Realtime, CONNECTED, RECONNECT, DISCONNECTED, MESSAGE_RESEND } from "..
2
2
 
3
3
  async function run(){
4
4
  var realtime = new Realtime({
5
- api_key: process.env.user_key,
6
- secret: process.env.secret
5
+ api_key: "eyJ0eXAiOiJKV1QiLCJhbGciOiJlZDI1NTE5L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  });
8
8
  await realtime.init(true, {
9
9
  max_retries: 2,
10
- // debug: true
10
+ debug: true
11
11
  });
12
12
 
13
13
  realtime.on(CONNECTED, async () => {
@@ -16,9 +16,9 @@ async function run(){
16
16
  for (let angle = 0; angle <= 18000; angle++){
17
17
 
18
18
  var value = Math.floor(Math.random() * (100 + 1))
19
- console.log(value)
19
+ // console.log(value)
20
20
 
21
- // await realtime.publish("test-power-1", {
21
+ // await realtime.publish("test-power", {
22
22
  // "value": value,
23
23
  // "time": Date.now() + 2000
24
24
  // })
@@ -28,7 +28,7 @@ async function run(){
28
28
  "time": Date.now()
29
29
  });
30
30
 
31
- console.log(`Message sent => ${sent}`);
31
+ // console.log(`Message sent => ${sent}`);
32
32
 
33
33
  await realtime.sleep(100)
34
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayx-js",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -114,7 +114,8 @@ export class Realtime {
114
114
  "nats://0.0.0.0:4223",
115
115
  "nats://0.0.0.0:4224",
116
116
  "nats://0.0.0.0:4225",
117
- "nats://0.0.0.0:4226"] :
117
+ "nats://0.0.0.0:4226"
118
+ ] :
118
119
  [
119
120
  "nats://api.relay-x.io:4221",
120
121
  "nats://api.relay-x.io:4222",
@@ -185,7 +186,7 @@ export class Realtime {
185
186
  reconnect: true,
186
187
  reconnectTimeWait: 1000,
187
188
  authenticator: credsAuth,
188
- token: this.api_key
189
+ token: this.api_key,
189
190
  });
190
191
 
191
192
  this.#jsManager = await jetstreamManager(this.#natsClient);
@@ -279,6 +280,7 @@ export class Realtime {
279
280
 
280
281
  // Subscribe to topics
281
282
  this.#subscribeToTopics();
283
+ this.#log("Subscribed to topics");
282
284
  }
283
285
  }
284
286
 
@@ -358,7 +360,7 @@ export class Realtime {
358
360
  }
359
361
 
360
362
  if (![CONNECTED, DISCONNECTED, RECONNECT, this.#RECONNECTED,
361
- this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND].includes(topic)){
363
+ this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND, SERVER_DISCONNECT].includes(topic)){
362
364
  if(!this.isTopicValid(topic)){
363
365
  // We have an invalid topic, lets remove it
364
366
  if(topic in this.#event_func){
@@ -485,6 +487,8 @@ export class Realtime {
485
487
  end = end.toISOString();
486
488
  }
487
489
 
490
+ console.log(`END => ${end}`)
491
+
488
492
  await this.#createOrGetStream();
489
493
 
490
494
  var opts = {
@@ -501,9 +505,7 @@ export class Realtime {
501
505
 
502
506
  this.#consumerMap[topic] = consumer;
503
507
 
504
- const msgs = await consumer.fetch({
505
- expires: 10000
506
- });
508
+ const msgs = await consumer.consume();
507
509
 
508
510
  var history = [];
509
511
 
@@ -516,6 +518,8 @@ export class Realtime {
516
518
  }
517
519
  }
518
520
 
521
+ console.log(m.timestamp)
522
+
519
523
  var data = m.json();
520
524
  history.push(data.message);
521
525
  }
@@ -578,12 +582,12 @@ export class Realtime {
578
582
 
579
583
  const consumer = await this.#jetstream.consumers.get(this.#getStreamName(), opts);
580
584
  this.#log(this.#topicMap)
581
- this.#log("Consumer is consuming");
582
585
 
583
586
  this.#consumerMap[topic] = consumer;
584
587
 
585
588
  await consumer.consume({
586
589
  callback: (msg) => {
590
+ console.log("TIMESTAMP", msg.info)
587
591
 
588
592
  msg.ack();
589
593
  try{
@@ -607,6 +611,7 @@ export class Realtime {
607
611
  }
608
612
  }
609
613
  });
614
+ this.#log("Consumer is consuming");
610
615
  }
611
616
 
612
617
  /**
@@ -650,6 +655,7 @@ export class Realtime {
650
655
  await this.#jsManager.streams.add({
651
656
  name: streamName,
652
657
  subjects: [...this.#getStreamTopicList(), ...this.#getPresenceTopics()],
658
+ num_replicas: 3
653
659
  });
654
660
 
655
661
  this.#log(`${streamName} created`);
@@ -675,9 +681,9 @@ export class Realtime {
675
681
  isTopicValid(topic){
676
682
  if(topic !== null && topic !== undefined && (typeof topic) == "string"){
677
683
  var arrayCheck = ![CONNECTED, DISCONNECTED, RECONNECT, this.#RECONNECTED,
678
- this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND].includes(topic);
684
+ this.#RECONNECTING, this.#RECONN_FAIL, MESSAGE_RESEND, SERVER_DISCONNECT].includes(topic);
679
685
 
680
- var spaceStarCheck = !topic.includes(" ") && !topic.includes("*");
686
+ var spaceStarCheck = !topic.includes(" ") && !topic.includes("*") && !topic.includes(".");
681
687
 
682
688
  return arrayCheck && spaceStarCheck;
683
689
  }else{
@@ -859,4 +865,5 @@ export class Realtime {
859
865
  export const CONNECTED = "CONNECTED";
860
866
  export const RECONNECT = "RECONNECT";
861
867
  export const MESSAGE_RESEND = "MESSAGE_RESEND";
862
- export const DISCONNECTED = "DISCONNECTED";
868
+ export const DISCONNECTED = "DISCONNECTED";
869
+ export const SERVER_DISCONNECT = "SERVER_DISCONNECT";