relayx-js 1.0.10 → 1.0.11

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.11",
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){
@@ -369,10 +374,10 @@ export class Realtime {
369
374
  this.#topicMap.push(topic);
370
375
  }
371
376
 
372
- if(this.connected){
373
- // Connected we need to create a topic in a stream
374
- await this.#startConsumer(topic);
375
- }
377
+ if(this.connected){
378
+ // Connected we need to create a topic in a stream
379
+ await this.#startConsumer(topic);
380
+ }
376
381
  }
377
382
 
378
383
  return true;
@@ -562,6 +567,8 @@ export class Realtime {
562
567
  * @param {string} topic
563
568
  */
564
569
  async #startConsumer(topic){
570
+ this.#log(`Starting consumer for topic: ${topic}_${uuidv4()}`)
571
+
565
572
  var opts = {
566
573
  name: `${topic}_${uuidv4()}`,
567
574
  filter_subjects: [this.#getStreamTopic(topic), this.#getStreamTopic(topic) + "_presence"],
@@ -577,16 +584,15 @@ export class Realtime {
577
584
  this.#consumerMap[topic] = consumer;
578
585
 
579
586
  await consumer.consume({
580
- callback: (msg) => {
587
+ callback: async (msg) => {
581
588
  try{
589
+ const now = Date.now();
582
590
  this.#log("Decoding msgpack message...")
583
591
  var data = decode(msg.data);
584
592
 
585
593
  var room = data.room;
586
594
 
587
595
  this.#log(data);
588
- const latency = Date.now() - data.start
589
- this.#log(`Latency => ${latency}`)
590
596
 
591
597
  // Push topic message to main thread
592
598
  if (room in this.#event_func && data.client_id != this.#getClientId()){
@@ -597,6 +603,8 @@ export class Realtime {
597
603
  }
598
604
 
599
605
  msg.ack();
606
+
607
+ await this.#logLatency(now, data);
600
608
  }catch(err){
601
609
  this.#log("Consumer err " + err);
602
610
  msg.nack(5000);
@@ -626,11 +634,95 @@ export class Realtime {
626
634
  return del;
627
635
  }
628
636
 
637
+ async #logLatency(now, data){
638
+ if(data.client_id == this.#getClientId()){
639
+ this.#log("Skipping latency log for own message");
640
+ return;
641
+ }
642
+
643
+ if(this.#latency.length >= 100){
644
+ this.#log("Latency array is full, skipping log");
645
+ return;
646
+ }
647
+
648
+ const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
649
+
650
+ this.#log(`Timezone: ${timeZone}`);
651
+
652
+ const latency = now - data.start
653
+ this.#log(`Latency => ${latency}`)
654
+
655
+ this.#latency.push({
656
+ latency: latency,
657
+ timestamp: now
658
+ });
659
+
660
+ if(this.#latencyPush == null){
661
+ this.#latencyPush = setTimeout(async () => {
662
+ this.#log("setTimeout called");
663
+
664
+ if(this.#latency.length > 0){
665
+ this.#log("Push from setTimeout")
666
+ await this.#pushLatencyData({
667
+ timezone: timeZone,
668
+ history: this.#latency,
669
+ });
670
+ }else{
671
+ this.#log("No latency data to push");
672
+ }
673
+
674
+ }, 30000);
675
+ }
676
+
677
+ if(this.#latency.length == 100 && !this.#isSendingLatency){
678
+ this.#log("Push from Length Check: " + this.#latency.length);
679
+ await this.#pushLatencyData({
680
+ timezone: timeZone,
681
+ history: this.#latency,
682
+ });
683
+ }
684
+ }
685
+
629
686
  // Utility functions
630
687
  #getClientId(){
631
688
  return this.#natsClient?.info?.client_id
632
689
  }
633
690
 
691
+ async #pushLatencyData(data){
692
+ this.#isSendingLatency = true;
693
+
694
+ try{
695
+ var res = await this.#natsClient.request("accounts.user.log_latency",
696
+ JSONCodec().encode({
697
+ api_key: this.api_key,
698
+ payload: data
699
+ }),
700
+ {
701
+ timeout: 5000
702
+ }
703
+ )
704
+
705
+ var data = res.json()
706
+
707
+ this.#log(data)
708
+ this.#resetLatencyTracker();
709
+ }catch(err){
710
+ this.#log("Error getting pushing latency data")
711
+ this.#log(err);
712
+ }
713
+
714
+ this.#isSendingLatency = false;
715
+ }
716
+
717
+ #resetLatencyTracker(){
718
+ this.#latency = [];
719
+
720
+ if(this.#latencyPush != null){
721
+ clearTimeout(this.#latencyPush);
722
+ this.#latencyPush = null;
723
+ }
724
+ }
725
+
634
726
  /**
635
727
  * Checks if a topic can be used to send messages to.
636
728
  * @param {string} topic - Name of event
@@ -716,7 +808,6 @@ export class Realtime {
716
808
 
717
809
  output = await func(...args);
718
810
  success = output.success;
719
- // this.#log(output);
720
811
 
721
812
  methodDataOutput = output.output;
722
813