WebSockets – NodeJS

Introduction

To connect using WebSockets API, you will need following information :
 
– “endpoint” to connect to,
– “port number” to connect to,
– “API Key”
 
You will get all of the above once you purchase the API from our team
 
The flow of operations should be as follows :
 
1. Make a connection to the ws://endpoint:port
2. Send Authentication Request using API Key
3. Once authentication is successful, send all other data requests
4. If connection is lost for any reasons, follow same steps from 1 to 3 as above.
 
Ready, lets gets started then
 

Download the sample

– Download NodeJS Sample from Download Code Samples page.
– The sample is zipped. Once downloaded, extract it to some folder using WinZip or WinRar
– Open the WebsocketSample_Phython.py in the editor. You can use Visual Studio Code, Notepad++ or any compatible Editor
 
Note the code below in file at the top :
 

"""
/*
GFDL TODO - please enter below the endpoint received from GFDL team. 
If you dont have one, please contact us on sales@globaldatafeeds.in 
*/
var endpoint = "ws://endpoint:port/";

/*
//GFDL TODO - please enter below the API Key received from GFDL team. 
If you dont have one, please contact us on sales@globaldatafeeds.in 
*/
var accesskey = "Enter Your API Key Here";

 
– Enter the Endpoint, Port and API Access Key received from our team in above code.
– Save this file and run it.
– Make sure you run the sample during market hours.

What happens when sample is run for the first time ?

– When sample is run for the first time, it sends request to server for SubscribeRealtime function (NIFTY-I Symbol from NFO Exchange). SubscribeRealtime pushes realtime data of requested symbol from server to client i.e. to you. If your account is not enabled for this Exchange, kindly make suitable changes in async def SubscribeRealtime() function and replace ExchangeName & InstIdentifier variables. The sample code has examples of symbols from other Exchanges for quick reference. For complete reference, please see Symbol Naming Conventions.
– If you run sample during market hours, you will notice updation of realtime data of available symbol at your end.
– Which function to call – is controlled in function functionCall() function. Here, you can uncomment the required function to see it in action (one function at a time).
– The code below will be run when sample is run :
 

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/* 	GFDL : 	1. 	SubscribeRealtime : pushes realtime data every second for the subscribed instrument from server
				2. 	So you will need to send this request only once and subscribe for the data.	
				3.	Please note that if there is internet disconnection for any reason, you will need to 
					subscribe to all the instruments again - to receive the data.
				4.	To see this function in action, you should run it during market hours
				5.	If you want to subscribe to data of multiple symbols, you will need to send 1 request each - for each symbol
				6. 	Please see symbol naming conventions here : https://globaldatafeeds.in/global-datafeeds-apis/global-datafeeds-apis/documentation-support/symbol-naming-convention/
	*/
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
Subscribing to realtime data of Futures & Options :
---------------------------------------------------
//Below request will subscribe to realtime data of Current Month Futures of NIFTY	
//This example shows how to request data using Continuous Format
//Similarly, you can send NIFTY-II (Near month), NIFTY-III (Far month). 
//Below Symbol is Continuous Format of NIFTY Futures. It will never expire. So no change in code will be necessary.
//You can use same naming convention for Futures of Instruments from NFO, CDS, MCX Exchanges
//CDS Examples : USDINR-I, USDINR-II, USDINR-III
//MCX Examples : NATURALGAS-I, NATURALGAS-II, NATURALGAS-III

//Similarly, you can send NIFTY20AUGFUT (near month), NIFTY20SEPFUT (far month). 
//You can use same naming convention for Futures of Instruments from NFO, CDS, MCX Exchanges
//NFO Options Examples : NIFTY02JUL2010000CE, RELIANCE30JUL201700CE
//CDS Futures Examples : USDINR20JULFUT, USDINR20AUGFUT, USDINR20SEPFUT
//CDS Options Examples : USDINR29JUL2075.5CE, EURINR29JUL2080CE
//MCX Options Examples : CRUDEOIL20JULFUT, CRUDEOIL20AUGFUT, CRUDEOIL20SEPFUT
//MCX Options Examples : CRUDEOIL20JUL2050PE, GOLD20JUL43700PE	
//Important : Replace it with appropriate expiry date if this contract is expired

//Similarly, you can send FUTIDX_NIFTY_27AUG2020_XX_0 (near month), FUTIDX_NIFTY_24SEP2020_XX_0 (far month). 
//You can use same naming convention for Futures of Instruments from NFO, CDS, MCX Exchanges
//NFO Options Examples : OPTIDX_NIFTY_02JUL2020_CE_10000, OPTSTK_RELIANCE_30JUL2020_CE_1700
//CDS Futures Examples : FUTCUR_USDINR_26JUN2020_XX_0, FUTCUR_USDINR_29JUL2020_XX_0, FUTCUR_USDINR_27AUG2020_XX_0
//CDS Options Examples : OPTCUR_USDINR_29JUL2020_CE_75.5, OPTCUR_EURINR_29JUL2020_CE_80
//MCX Futures Examples : FUTCOM_CRUDEOIL_20JUL2020__0, FUTCOM_CRUDEOIL_19AUG2020__0, FUTCOM_CRUDEOIL_21SEP2020__0
//MCX Options Examples : OPTFUT_CRUDEOIL_16JUL2020_PE_2050, OPTFUT_GOLD_27JUL2020_PE_43700
//Important : Replace it with appropriate expiry date if this contract is expired

Subscribing to realtime data of NSE Indices
-------------------------------------------
//Use InstrumentIdenfier value "NIFTY 50", "NIFTY BANK", "NIFTY 100", etc.
//Use NSE_IDX as Exchange
//Please note that Indices Symbols have white space. For example, between NIFTY & 50, NIFTY & BANK above

Subscribing to realtime data of NSE Stocks 
------------------------------------------
//For EQ Series, use InstrumentIdentifier value BAJAJ-AUTO, RELIANCE, AXISBANK, LT, etc..
//To subscribe to realtime data of any other series, append the series name to symbol name 
//for example, to request data of RELIANCE CAPITAL from BE Series, use RELCAPITAL.BE
//EQ Series Symbols are mentioned without any suffix

// Please see symbol naming conventions here : 
// https://globaldatafeeds.in/global-datafeeds-apis/global-datafeeds-apis/documentation-support/symbol-naming-convention/
*/

function SubscribeRealtime()
{
	if (connection.connected) 
	{
		var ExchangeName = "NFO";				//GFDL : Supported values : NSE (stocks), NSE_IDX (Indices), NFO (F&O), MCX & CDS (Currency)
		var InstIdentifier = "NIFTY-I";			//GFDL : NIFTY-I always represents current month Futures. 
		request = '{"MessageType":"SubscribeRealtime","Exchange":"' + ExchangeName + '","InstrumentIdentifier":"' + InstIdentifier + '"}';
		callAPI(request);
	}
}

Sample Response

D:\node-server>node WebsocketSample_NodeJS.js
Client Connected!
request: *****{"MessageType":"Authenticate","Password":"API_Key_is_Masked"}*****
request: *****{"MessageType":"SubscribeRealtime","Exchange":"NFO","InstrumentIdentifier":"NIFTY-I"}*****
Received: '{"MessageType":"Echo"}'
Received: '{"Complete":true,"Message":"Welcome!","MessageType":"AuthenticateResult"}'
Received: '{"AllowVMRunning":false,"MessageType":"AllowVMRunningResult"}'
Received: '{"AllowServerOSRunning":false,"MessageType":"AllowServerOSRunningResult"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112003,"ServerTime":1594112004,"AverageTradedPrice":10717.52,"BuyPrice":10740.0,"BuyQty":1350,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10740.0,"LastTradeQty":150,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10740.85,"SellQty":75,"TotalQtyTraded":11468850,"Value":122917629252.0,"PreOpen":false,"PriceChange":-14.85,"PriceChangePercentage":-0.14,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112004,"ServerTime":1594112005,"AverageTradedPrice":10717.52,"BuyPrice":10740.05,"BuyQty":75,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10740.5,"LastTradeQty":1050,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10741.5,"SellQty":75,"TotalQtyTraded":11469900,"Value":122928882648.0,"PreOpen":false,"PriceChange":-14.35,"PriceChangePercentage":-0.13,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"MessageType":"Echo"}'
request: *****{"MessageType":"SubscribeRealtime","Exchange":"NFO","InstrumentIdentifier":"NIFTY-I"}*****
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112005,"ServerTime":1594112006,"AverageTradedPrice":10717.52,"BuyPrice":10739.0,"BuyQty":75,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10739.65,"LastTradeQty":3450,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10740.0,"SellQty":3600,"TotalQtyTraded":11473350,"Value":122965858092.0,"PreOpen":false,"PriceChange":-15.2,"PriceChangePercentage":-0.14,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112006,"ServerTime":1594112007,"AverageTradedPrice":10717.52,"BuyPrice":10738.35,"BuyQty":150,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10739.0,"LastTradeQty":150,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10739.9,"SellQty":300,"TotalQtyTraded":11473500,"Value":122967465720.0,"PreOpen":false,"PriceChange":-15.85,"PriceChangePercentage":-0.15,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"MessageType":"Echo"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112007,"ServerTime":1594112008,"AverageTradedPrice":10717.52,"BuyPrice":10738.15,"BuyQty":75,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10738.35,"LastTradeQty":225,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10739.75,"SellQty":75,"TotalQtyTraded":11473725,"Value":122969877162.0,"PreOpen":false,"PriceChange":-16.5,"PriceChangePercentage":-0.15,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112008,"ServerTime":1594112009,"AverageTradedPrice":10717.53,"BuyPrice":10738.15,"BuyQty":150,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10738.35,"LastTradeQty":75,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10739.95,"SellQty":75,"TotalQtyTraded":11473800,"Value":122970795714.0,"PreOpen":false,"PriceChange":-16.5,"PriceChangePercentage":-0.15,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"MessageType":"Echo"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112009,"ServerTime":1594112010,"AverageTradedPrice":10717.53,"BuyPrice":10738.35,"BuyQty":75,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10738.5,"LastTradeQty":975,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10739.95,"SellQty":75,"TotalQtyTraded":11474775,"Value":122981245305.75,"PreOpen":false,"PriceChange":-16.35,"PriceChangePercentage":-0.15,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"Exchange":"NFO","InstrumentIdentifier":"NIFTY-I","LastTradeTime":1594112010,"ServerTime":1594112011,"AverageTradedPrice":10717.53,"BuyPrice":10738.65,"BuyQty":75,"Close":10754.85,"High":10767.5,"Low":10657.7,"LastTradePrice":10740.0,"LastTradeQty":375,"Open":10761.9,"OpenInterest":12112200,"QuotationLot":0.0,"SellPrice":10740.0,"SellQty":3525,"TotalQtyTraded":11475150,"Value":122985264379.5,"PreOpen":false,"PriceChange":-14.85,"PriceChangePercentage":-0.14,"OpenInterestChange":874500,"MessageType":"RealtimeResult"}'
Received: '{"MessageType":"Echo"}'

– To know how each function works, you can uncomment required function at top (one function at a time), save the sample and run it.
 

Trouble-shooting

The code outputs nothing

– Please make sure you have entered the Endpoint, Port & API Key in the sample code, saved the changes before running it.
– To see realtime data in action, you should run the sample during market hours
– If you are behind firewall, please speak to your network administrator and get the endpoint URL and port whitelisted in your firewall
 

I can see some output but I dont see all symbols requested / I get “Exchange Not Enabled.” message

– SubscribeRealtime function requests data of a symbol (NIFTY-I from NFO Exchange in the unedited sample). You will need to make sure you have paid for the subscription of the Exchange you are using in your request.
 
– Some examples use Contractwise symbols which might have expired. Please check code and change the symbols. Please check Symbol Naming Conventions page for more information about various ways to name symbols (especially Futures and Options).
 

I have requested data but it takes lot of time to receive the response

– If you have requested entire GetHistory, GetInstruments (for NSE / NFO), GetExchangeSnapshot or GetLastQuoteOptionChain, these functions return thousands of rows. So it is possible that it takes more time than other requests to return the data
 
– Please make sure you are on fast internet connection.
 

I have other issues not listed here

– Please drop an email to developer@globaldatafeeds.in with following :
1. Details of the request sent (exact syntax), expected response & received response
2. Screenshot of the request & response
3. API Key used
 
Once received, we will check and revert at the earliest (typically within few hours for normal queries).

×

Was this helpful?