consumer_1.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { SearchService } from "../services/query.service"
  2. import _ = require("lodash")
  3. import { Subject, interval } from "rxjs";
  4. import * as WebSocket from 'ws';
  5. /* ---------------------- COMPLEX OPERATION ------------------------------ */
  6. // Check Heap size
  7. // const v8 = require('v8');
  8. // const heapStats = v8.getHeapStatistics();
  9. // const maxHeapSize = heapStats.heap_size_limit / (1024 * 1024); // Convert to MB
  10. // console.log(`Current maximum heap size: ${maxHeapSize.toFixed(2)} MB`);
  11. // Create new Subject to handle incoming data from remote subscription
  12. let payload: Subject<any> = new Subject()
  13. payload.subscribe((element) => {
  14. console.log(`Received message from server: ${element.appData.msgId}`);
  15. const used = process.memoryUsage().heapUsed / 1024 / 1024;
  16. console.log(`Heap used: ${used} MB`);
  17. })
  18. // Create a new WebSocket client
  19. const ws = new WebSocket('ws://localhost:8080');
  20. // Listen for the WebSocket connection to open
  21. ws.on('open', () => {
  22. console.log('Connected to WebSocket server');
  23. // Send a message to the server
  24. ws.send('Hello, server!');
  25. });
  26. // Listen for messages from the server
  27. ws.on('message', (message: string) => {
  28. let msgObj = JSON.parse(message)
  29. payload.next(msgObj)
  30. });
  31. // Listen for the WebSocket connection to close
  32. ws.on('close', () => {
  33. console.log('Disconnected from WebSocket server');
  34. });