finnhubio / Finnhub-API

Finnhub API provides institutional-grade financial data to investors, fintech startups and investment firms. We support real-time stock price, global fundamentals and alternative data. https://finnhub.io/docs/api
98 stars 6 forks source link

Websocket closes connection after few ping messages #520

Open Becherman opened 1 year ago

Becherman commented 1 year ago

After successful open event server sends me ping messages. After very random number such messages connection closes despite i emit pong in response

Code example


const WebSocket = require("ws");
const socket = new WebSocket(
  "wss://ws.finnhub.io?token=<my-api-token>"
);

// Connection opened -> Subscribe
socket.addEventListener("open", function (event) {
  console.log("WebSocket is opened");
  socket.send(JSON.stringify({ type: "subscribe-pr", symbol: "AAPL" }));
  socket.send(JSON.stringify({ type: "subscribe-pr", symbol: "MSFT" }));
  socket.send(JSON.stringify({ type: "subscribe-pr", symbol: "AMZN" }));
});

// Listen for messages
socket.addEventListener("message", function (event) {
  console.log("Message from server ", event.data, new Date().toISOString());

  if (event.data.type === 'ping') {
    socket.send(JSON.stringify({ type: 'pong' }))
  }
});

socket.addEventListener("close", () => {
  console.log("Connection closed");
});

// Unsubscribe
var unsubscribe = function (symbol) {
  socket.send(JSON.stringify({ type: "unsubscribe-pr", symbol: symbol }));
};
mcigorli commented 1 year ago

Raising the topic! I have exactly the same problem! Will anyone help?

Zedron commented 10 months ago

Same issue although I'm using Java not python. Closes with code 1006 randomly. I've gotten around it by reconnecting when it closes unexpectedly, but not a great solution.

bacarin commented 1 month ago

bumping up , having the same problem in java implementation

jjjachyty commented 1 month ago

+1 golang

skyheights commented 2 weeks ago

+1 golang. connection just goes silent. gorilla client returns 1006