Skip to content

Releases: zerodha/kiteconnectjs

Autoreconnect feature for WebSocket client

12 Mar 10:43
Compare
Choose a tag to compare

Auto re-connect WebSocket client

Optionally you can enable client side auto reconnection to automatically reconnect if the connection is dropped.
It is very useful at times when client side network is unreliable and patchy.

All you need to do is enable auto reconnection with preferred interval and time. For example

// Enable auto reconnect with 5 second interval and retry for maximum of 20 times.
ticker.autoReconnect(true, 20, 5)

// You can also set reconnection times to -1 for inifinite reconnections
ticker.autoReconnect(true, -1, 5)
  • Event reconnecting is called when auto reconnection is triggered and event callback carries two additional params reconnection interval set and current reconnection count.

  • Event noreconnect is called when number of auto reconnections exceeds the maximum reconnection count set. For example if maximum reconnection count is set as 20 then after 20th reconnection this event will be triggered. Also note that the current process is exited when this event is triggered.

  • Event connect will be triggered again when reconnection succeeds.

Here is an example demonstrating auto reconnection.

var KiteTicker = require("kiteconnect").KiteTicker;
var ticker = new KiteTicker(api_key, user_id, public_token);

// set autoreconnect with 10 maximum reconnections and 5 second interval
ticker.autoReconnect(true, 10, 5)
ticker.connect();
ticker.on("tick", setTick);
ticker.on("connect", subscribe);

ticker.on("noreconnect", function() {
	console.log("noreconnect")
});

ticker.on("reconnecting", function(reconnect_interval, reconnections) {
	console.log("Reconnecting: attempet - ", reconnections, " innterval - ", reconnect_interval);
});

function setTick(ticks) {
	console.log("Ticks", ticks);
}

function subscribe() {
	var items = [738561];
	ticker.subscribe(items);
	ticker.setMode(ticker.modeFull, items);
}

v1.1.0

28 Nov 07:08
Compare
Choose a tag to compare

Added disconnect method for websocket.

v1.0.0

25 Oct 05:47
Compare
Choose a tag to compare
Fixed docs