Using Effect to Manage WebSocket Connections in TypeScript

Hi, I could need some help/hints to wrap my head around.

I am experienced developer, but not in typescript, asyn, websockets ... ^ effect.ts

Is it of use to manage a websocket with effect?

I guess I would wrap a websocket in a Layer,
that may have a subscription function and emits events?

Also it may be automatically reopened when broken or closed, and be and cancelled

Here is the example I am working with.
It is

import WebSocket from "ws";
const socket = new WebSocket("wss://stream.aisstream.io/v0/stream");

socket.onopen = function (_) {
  let subscriptionMessage = {
    Apikey: "...",
    BoundingBoxes: [
      [
        [-180, -90],
        [180, 90],
      ],
    ],
  };
  socket.send(JSON.stringify(subscriptionMessage));
};

socket.onmessage = function (event) {
  console.log();
  console.log(event.data.toString());
  socket.close();
};

I am thankful for any sugestions

Frank
Was this page helpful?