index.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* eslint-disable no-console */
  2. /* eslint-disable @typescript-eslint/no-empty-function */
  3. import { PythLazerClient } from "../src/index.js";
  4. // Ignore debug messages
  5. console.debug = () => {};
  6. const client = await PythLazerClient.create({
  7. urls: [
  8. "wss://pyth-lazer-0.dourolabs.app/v1/stream",
  9. "wss://pyth-lazer-1.dourolabs.app/v1/stream",
  10. ],
  11. token: "you-access-token-here", // Replace with your actual access token
  12. numConnections: 4, // Optionally specify number of parallel redundant connections to reduce the chance of dropped messages. The connections will round-robin across the provided URLs. Default is 4.
  13. logger: console, // Optionally log socket operations (to the console in this case.)
  14. onError: (error) => {
  15. console.error("WebSocket error:", error);
  16. },
  17. // Optional configuration for resilient WebSocket connections
  18. rwsConfig: {
  19. heartbeatTimeoutDurationMs: 5000, // Optional heartbeat timeout duration in milliseconds
  20. maxRetryDelayMs: 1000, // Optional maximum retry delay in milliseconds
  21. logAfterRetryCount: 10, // Optional log after how many retries
  22. },
  23. });
  24. // Read and process messages from the Lazer stream
  25. client.addMessageListener((message) => {
  26. console.info("got message:", message);
  27. switch (message.type) {
  28. case "json": {
  29. if (message.value.type == "streamUpdated") {
  30. console.info(
  31. "stream updated for subscription",
  32. message.value.subscriptionId,
  33. ":",
  34. message.value.parsed?.priceFeeds,
  35. );
  36. }
  37. break;
  38. }
  39. case "binary": {
  40. if ("solana" in message.value) {
  41. console.info("solana message:", message.value.solana?.toString("hex"));
  42. }
  43. if ("evm" in message.value) {
  44. console.info("evm message:", message.value.evm?.toString("hex"));
  45. }
  46. break;
  47. }
  48. }
  49. });
  50. // Monitor for all connections in the pool being down simultaneously (e.g. if the internet goes down)
  51. // The connections may still try to reconnect in the background. To shut down the client completely, call shutdown().
  52. client.addAllConnectionsDownListener(() => {
  53. console.error("All connections are down!");
  54. });
  55. // Create and remove one or more subscriptions on the fly
  56. client.subscribe({
  57. type: "subscribe",
  58. subscriptionId: 1,
  59. priceFeedIds: [1, 2],
  60. properties: ["price"],
  61. formats: ["solana"],
  62. deliveryFormat: "binary",
  63. channel: "fixed_rate@200ms",
  64. parsed: false,
  65. jsonBinaryEncoding: "base64",
  66. });
  67. client.subscribe({
  68. type: "subscribe",
  69. subscriptionId: 2,
  70. priceFeedIds: [1, 2, 3, 4, 5],
  71. properties: ["price", "exponent", "publisherCount", "confidence"],
  72. formats: ["evm"],
  73. deliveryFormat: "json",
  74. channel: "fixed_rate@200ms",
  75. parsed: true,
  76. jsonBinaryEncoding: "hex",
  77. });
  78. await new Promise((resolve) => setTimeout(resolve, 10_000));
  79. client.unsubscribe(1);
  80. client.unsubscribe(2);
  81. client.shutdown();