relayx-js 1.0.10 → 1.0.12

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.
@@ -8,10 +8,10 @@ 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: process.env.AUTH_JWT,
12
+ secret: process.env.AUTH_SECRET
13
13
  });
14
- await realtime.init(false, {
14
+ await realtime.init(true, {
15
15
  max_retries: 2,
16
16
  debug: true
17
17
  });
@@ -2,8 +2,8 @@ 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: process.env.AUTH_JWT,
6
+ secret: process.env.AUTH_SECRET
7
7
  });
8
8
  await realtime.init(true, {
9
9
  max_retries: 2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relayx-js",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "main": "realtime/realtime.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -35,6 +35,11 @@ export class Realtime {
35
35
  // Offline messages
36
36
  #offlineMessageBuffer = [];
37
37
 
38
+ // Latency
39
+ #latency = [];
40
+ #latencyPush = null;
41
+ #isSendingLatency = false;
42
+
38
43
  #maxPublishRetries = 5;
39
44
 
40
45
  constructor(config){
@@ -108,27 +113,18 @@ export class Realtime {
108
113
  this.#baseUrl = staging ? [
109
114
  "nats://0.0.0.0:4221",
110
115
  "nats://0.0.0.0:4222",
111
- "nats://0.0.0.0:4223",
112
- "nats://0.0.0.0:4224",
113
- "nats://0.0.0.0:4225",
114
- "nats://0.0.0.0:4226"
116
+ "nats://0.0.0.0:4223"
115
117
  ] :
116
118
  [
117
- "nats://api.relay-x.io:4221",
118
- "nats://api.relay-x.io:4222",
119
- "nats://api.relay-x.io:4223",
120
- "nats://api.relay-x.io:4224",
121
- "nats://api.relay-x.io:4225",
122
- "nats://api.relay-x.io:4226",
119
+ "tls://api.relay-x.io:4221",
120
+ "tls://api.relay-x.io:4222",
121
+ "tls://api.relay-x.io:4223"
123
122
  ];
124
123
  }else{
125
124
  this.#baseUrl = [
126
- "nats://api.relay-x.io:4221",
127
- "nats://api.relay-x.io:4222",
128
- "nats://api.relay-x.io:4223",
129
- "nats://api.relay-x.io:4224",
130
- "nats://api.relay-x.io:4225",
131
- "nats://api.relay-x.io:4226",
125
+ "tls://api.relay-x.io:4221",
126
+ "tls://api.relay-x.io:4222",
127
+ "tls://api.relay-x.io:4223"
132
128
  ];
133
129
  }
134
130
 
@@ -181,8 +177,8 @@ export class Realtime {
181
177
  this.#natsClient = await connect({
182
178
  servers: this.SEVER_URL,
183
179
  noEcho: true,
184
- maxReconnectAttempts: 1200,
185
180
  reconnect: true,
181
+ maxReconnectAttempts: 1200,
186
182
  reconnectTimeWait: 1000,
187
183
  authenticator: credsAuth,
188
184
  token: this.api_key,
@@ -212,6 +208,8 @@ export class Realtime {
212
208
 
213
209
  this.#natsClient.closed().then(() => {
214
210
  this.#log("the connection closed!");
211
+
212
+ this.#offlineMessageBuffer.length = 0;
215
213
  });
216
214
 
217
215
  (async () => {
@@ -286,6 +284,8 @@ export class Realtime {
286
284
  if(this.#natsClient !== null){
287
285
  this.reconnected = false;
288
286
  this.disconnected = true;
287
+
288
+ this.#offlineMessageBuffer.length = 0;
289
289
 
290
290
  this.#natsClient.close();
291
291
  }else{
@@ -369,10 +369,10 @@ export class Realtime {
369
369
  this.#topicMap.push(topic);
370
370
  }
371
371
 
372
- if(this.connected){
373
- // Connected we need to create a topic in a stream
374
- await this.#startConsumer(topic);
375
- }
372
+ if(this.connected){
373
+ // Connected we need to create a topic in a stream
374
+ await this.#startConsumer(topic);
375
+ }
376
376
  }
377
377
 
378
378
  return true;
@@ -562,6 +562,8 @@ export class Realtime {
562
562
  * @param {string} topic
563
563
  */
564
564
  async #startConsumer(topic){
565
+ this.#log(`Starting consumer for topic: ${topic}_${uuidv4()}`)
566
+
565
567
  var opts = {
566
568
  name: `${topic}_${uuidv4()}`,
567
569
  filter_subjects: [this.#getStreamTopic(topic), this.#getStreamTopic(topic) + "_presence"],
@@ -577,16 +579,15 @@ export class Realtime {
577
579
  this.#consumerMap[topic] = consumer;
578
580
 
579
581
  await consumer.consume({
580
- callback: (msg) => {
582
+ callback: async (msg) => {
581
583
  try{
584
+ const now = Date.now();
582
585
  this.#log("Decoding msgpack message...")
583
586
  var data = decode(msg.data);
584
587
 
585
588
  var room = data.room;
586
589
 
587
590
  this.#log(data);
588
- const latency = Date.now() - data.start
589
- this.#log(`Latency => ${latency}`)
590
591
 
591
592
  // Push topic message to main thread
592
593
  if (room in this.#event_func && data.client_id != this.#getClientId()){
@@ -597,6 +598,8 @@ export class Realtime {
597
598
  }
598
599
 
599
600
  msg.ack();
601
+
602
+ await this.#logLatency(now, data);
600
603
  }catch(err){
601
604
  this.#log("Consumer err " + err);
602
605
  msg.nack(5000);
@@ -626,11 +629,95 @@ export class Realtime {
626
629
  return del;
627
630
  }
628
631
 
632
+ async #logLatency(now, data){
633
+ if(data.client_id == this.#getClientId()){
634
+ this.#log("Skipping latency log for own message");
635
+ return;
636
+ }
637
+
638
+ if(this.#latency.length >= 100){
639
+ this.#log("Latency array is full, skipping log");
640
+ return;
641
+ }
642
+
643
+ const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
644
+
645
+ this.#log(`Timezone: ${timeZone}`);
646
+
647
+ const latency = now - data.start
648
+ this.#log(`Latency => ${latency}`)
649
+
650
+ this.#latency.push({
651
+ latency: latency,
652
+ timestamp: now
653
+ });
654
+
655
+ if(this.#latencyPush == null){
656
+ this.#latencyPush = setTimeout(async () => {
657
+ this.#log("setTimeout called");
658
+
659
+ if(this.#latency.length > 0){
660
+ this.#log("Push from setTimeout")
661
+ await this.#pushLatencyData({
662
+ timezone: timeZone,
663
+ history: this.#latency,
664
+ });
665
+ }else{
666
+ this.#log("No latency data to push");
667
+ }
668
+
669
+ }, 30000);
670
+ }
671
+
672
+ if(this.#latency.length == 100 && !this.#isSendingLatency){
673
+ this.#log("Push from Length Check: " + this.#latency.length);
674
+ await this.#pushLatencyData({
675
+ timezone: timeZone,
676
+ history: this.#latency,
677
+ });
678
+ }
679
+ }
680
+
629
681
  // Utility functions
630
682
  #getClientId(){
631
683
  return this.#natsClient?.info?.client_id
632
684
  }
633
685
 
686
+ async #pushLatencyData(data){
687
+ this.#isSendingLatency = true;
688
+
689
+ try{
690
+ var res = await this.#natsClient.request("accounts.user.log_latency",
691
+ JSONCodec().encode({
692
+ api_key: this.api_key,
693
+ payload: data
694
+ }),
695
+ {
696
+ timeout: 5000
697
+ }
698
+ )
699
+
700
+ var data = res.json()
701
+
702
+ this.#log(data)
703
+ this.#resetLatencyTracker();
704
+ }catch(err){
705
+ this.#log("Error getting pushing latency data")
706
+ this.#log(err);
707
+ }
708
+
709
+ this.#isSendingLatency = false;
710
+ }
711
+
712
+ #resetLatencyTracker(){
713
+ this.#latency = [];
714
+
715
+ if(this.#latencyPush != null){
716
+ clearTimeout(this.#latencyPush);
717
+ this.#latencyPush = null;
718
+ }
719
+ }
720
+
634
721
  /**
635
722
  * Checks if a topic can be used to send messages to.
636
723
  * @param {string} topic - Name of event
@@ -716,7 +803,6 @@ export class Realtime {
716
803
 
717
804
  output = await func(...args);
718
805
  success = output.success;
719
- // this.#log(output);
720
806
 
721
807
  methodDataOutput = output.output;
722
808