Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websockets state #13

Closed
wannesdemaeght opened this issue Jan 1, 2018 · 9 comments
Closed

Websockets state #13

wannesdemaeght opened this issue Jan 1, 2018 · 9 comments
Labels

Comments

@wannesdemaeght
Copy link

Is there anyway to determine the state of the websocket (closed, connecting, connected, ..)?

@balthazar
Copy link
Member

Not really, since it's possible to open multiple with one call, I only return the function to be able to close them. Why are you asking? Do you have connectivity issues?

@wannesdemaeght
Copy link
Author

wannesdemaeght commented Jan 1, 2018

no, none so far.
I was just hoping to use the connected state as a safeguard to interrupt my program. (and restart the websocket)

The way it is written now, on a random disconnect, the price I have stored for binance would just stop updating, but no warning what so ever.
This would be dangerous because I am comparing the price between multiple exchanges and make trades based on that price difference.

Example from another library I use:

const Bittrex = require("node-bittrex-api");
Bittrex.options({
	websockets: {
		onConnect: function() {
			console.log('Bittrex connected');
			Polling_Bittrex = 1;
		},
		onDisconnect: function() {
			console.log('Bitrex disconnected');
			Polling_Bittrex = 0;
			Bittrex_Websocket();
		}
	}
});

I then check for the Polling_Bittrex state in every iteration of my program.

@wannesdemaeght
Copy link
Author

Please note that the actual websocket call is not necessary to check the state of the websocket.
I only have 1 call for bittrex, but that doesn't matter for the safeguard.

@balthazar
Copy link
Member

balthazar commented Jan 1, 2018

Hum I see, not sure what would be the best here, maybe passing two optional callbacks as parameters. The issue is what to do when people are opening multiple channels.

@wannesdemaeght
Copy link
Author

wannesdemaeght commented Jan 2, 2018

This is my function of websocket feeds right now, how do you propose I would change it to have a sefeguard for the binance websocket?

	const Gdax_Websocket = new GdaxLib.WebsocketClient(Gdax_Market,"wss://ws-feed.gdax.com",null, {channels : [{"name" : "ticker"}]});
	Gdax_Websocket.on("message", data => { 
		if (data.type == "ticker") {
			for (x = 0; x < Number_of_Alts+1; x++) {
				if(data.product_id == Gdax_Market[x]) Gdax_BidPrice[x] = Number(data.best_bid)
			}
		}
	})
	Gdax_Websocket.on("open", data => {
		console.log("Websocket Gdax:        connected");
		Polling_Gdax = 1;
	})
	Gdax_Websocket.on("close", data => {
		Polling_Gdax = 0;
		console.log("Websocket Gdax:     disconnected")
		Gdax_Websocket();
	})
	
	const Bittrex_Websocket = new Bittrex.websockets.subscribe(Market_Bittrex.slice(1), function(data) {
		if (data.M === "updateExchangeState") {
			data.A.forEach(function(data_for) {
				for (x = 1; x < Number_of_Alts+1; x++) {
					if (data_for.MarketName == Market_Bittrex[x]) AskPrice_Bittrex[x] = data_for.Sells[0]["Rate"]
				}
				if (data_for.MarketName == "BTC-ZEC") ZEC_Price = data_for.Buys[0]["Rate"]
			})
		}
	})
	Bittrex.options({
		websockets: {
			onConnect: function() {
				console.log("Websocket Bittrex:     connected");
				Polling_Bittrex = 1;
			},
			onDisconnect: function() {
				console.log("Websocket Bittrex:  disconnected");
				Polling_Bittrex = 0;
				Bittrex_Websocket();
			}
		}
	});

	const Binance_Websocket = Binance.ws.ticker(Market_Binance.slice(1), ticker => {
		for (x = 1; x < Number_of_Alts+1; x++) {	
			if (ticker.symbol == Market_Binance[x]) AskPrice_Binance[x] = ticker.bestAsk
		}
	})

@wannesdemaeght
Copy link
Author

I have fixed the safeguard issue by checking the eventtime of the tickers. on every loop, the new eventtime must be greater than the last. So that's working.

Only thing left to do is reopen websocket when it's been dropped, does this library do this?

@balthazar
Copy link
Member

Nope, what I think I'll end up doing is simply having two optional callbacks in each method that people can use themselves. It wouldn't really work if they open multiple sockets at once but it should be something they take care about too.

@wannesdemaeght
Copy link
Author

Today seems to be a bad day for binance websockets, but a great day for testing this library:

events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: connect ETIMEDOUT 52.199.118.147:9443
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)

This also confirms that this library does not reopen dropped websockets, or handle timeouts properly.
Is there something we can do on our end to ensure this does not crash node (and preferably, retry to establish connection)?

@bennycode
Copy link
Contributor

@balthazar When we return individual WS connections (#62), then it would be possible to iterate over them and query them for their state.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants