User gets connected from websocket in first attempt - web socket

Hi Members,
Whenever I try to connect wit the paytm money websocket User gets connected from websocket in first attempt after that attempt it works in every attempt.

here is my javascript code.

  const socket = new WebSocket(
    `wss://developer-ws.paytmmoney.com/broadcast/user/v1/data?x_jwt_token=${EnvironmentVariables.public_access_token}`
  );

  // Event triggered when user connection has successfully opened
  socket.addEventListener("open", function () {
    if (socket.readyState === socket.OPEN) {
      socket.send(
        JSON.stringify([
          {
            actionType: "ADD",
            modeType: "FULL",
            scripType: "INDEX",
            exchangeType: "NSE",
            scripId: "25",
          },
        ])
      );
    }
  });

  // Event triggered when user connection has closed
  socket.addEventListener("close", function (event) {
    console.log("User has been disconnected");
  });

  // Event triggered when user connection gets an error
  socket.addEventListener("error", function (event) {
    console.log("[ERROR] - Error occured in bank nifty live socket", event);
  });

  // Event triggered when user connection gets a message from the server
  socket.addEventListener("message", function (message) {
    try {
      let data: Buffer;
      data = <Buffer>message.data;
      let l = data.byteLength;
      let dvu = new DataView(bufferToArrayBuffer(data));
      let position = 0;
      while (position != l) {
        var type = dvu.getInt8(position);
        position = position + 1;
        switch (type) {
          case 66:
            position = processIndexQuotePacket(dvu, position);
            writeFileSync(
              `data/backup-live/livedata.json`,
              JSON.stringify(liveData),
              {
                flag: "w",
              }
            );
          default:
            console.log("Default");
            break;
        }
      }
    } catch (error) {
      console.log("[ERROR] - occurred in message addEventListener", error);
    }
  });
};

1 Like

Hi @adityatoke, I ran your code on my system and it is working completely fine. It would help us in resolving the issue if you could further clarify it.

1 Like

try using writeFileAsync instead of writeFileSync as it will write on each socket.

or avoid using writeFileSync on each socket. Your system may crash if you write on each socket events

use SetInterval and every minute you can write instead of each socket events

I have not tested your code but just a suggestion.

@adityatoke @rajilesh

Can you please let me know whether you are getting empty {{ Type }} parameter in the response or not.

Or can you send me a photo of the response?

In my case I get the response but I don’t understand how to use it. Can you help me by answering my question.