WebSockets – Java

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 Java Sample from Download Code Samples page.
– The sample is zipped. Once downloaded, extract it to some folder using WinZip or WinRar
– Open the Project in the editor. You can use Eclipse IDE for Java Developers (free to download) 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 
        String 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 
        String 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. You can run it directly in the editor (if supported) or you can run it in your browser.
– 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 (1 symbol each from each Exchange). SubscribeRealtime pushes realtime data of requested symbol from server to client i.e. to you. The sample requests realtime data of various symbols to the server.
– If you run sample during market hours, you will notice updation of realtime data of available symbols at your end.
– Which function to run – is controlled by main() and Switch-Case statement – as shown below :
 

public class WebSocketSample_JAVA {
    public static void main(String[] args) throws Exception {
        
    	//GFDL TODO - please enter below the endpoint received from GFDL team. If you dont have one, please contact us on sales@globaldatafeeds.in 
        String 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 
        String AccessKey = "Enter your API Key here";

        
        /*GFDL TODO : All the functions supported by API are listed below. You can uncomment any function to see the flow of request and response*/
        
        String functionName = "SubscribeRealtime";
        //String functionName = "SubscribeSnapshot";

        //String functionName = "GetLastQuote";
        //String functionName = "GetLastQuoteShort";
        //String functionName = "GetLastQuoteShortWithClose";
        //String functionName = "GetLastQuoteArray";
        //String functionName = "GetLastQuoteArrayShort";
        //String functionName = "GetLastQuoteArrayShortWithClose";
        
        //String functionName = "GetSnapshot";
        //String functionName = "GetHistory";

        //String functionName = "GetExchanges";

        //String functionName = "GetInstrumentsOnSearch";
        //String functionName = "GetInstruments";

        //String functionName = "GetInstrumentTypes";
        //String functionName = "GetProducts";
        //String functionName = "GetExpiryDates";
        //String functionName = "GetOptionTypes";
        //String functionName = "GetStrikePrices";

        //String functionName = "GetServerInfo";
        //String functionName = "GetLimitation";

        //String functionName = "GetMarketMessages";
        //String functionName = "GetExchangeMessages";
        
        //String functionName = "GetLastQuoteOptionChain";
        //String functionName = "GetExchangeSnapshot";
    
        final WSClientEndpoint clientEndPoint;
        clientEndPoint = new WSClientEndpoint(new URI(EndPoint));
        
        //Here user is Authenticated using "getAuthenticate" function
        clientEndPoint.sendMessage(getAuthenticate("Authenticate",AccessKey));
        Thread.sleep(500);
        
        
        if(clientEndPoint.APIConnection)
        {       	
            switch (functionName) {
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			/* 	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/
			*/
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	            case "SubscribeRealtime":

Sample Response

{"MessageType":"Authenticate","Password":"password_is_masked"}
Authentication Complete!
Authentication Complete!
Authentication Complete!
{"MessageType":"SubscribeRealtime","Exchange":"NFO","InstrumentIdentifier":"NIFTY-I"}
{"MessageType":"SubscribeRealtime","Exchange":"NFO","InstrumentIdentifier":"FUTIDX_NIFTY_30JUL2020_XX_0"}
{"MessageType":"SubscribeRealtime","Exchange":"NFO","InstrumentIdentifier":"NIFTY20JULFUT"}
{"MessageType":"SubscribeRealtime","Exchange":"NSE_IDX","InstrumentIdentifier":"NIFTY 50"}
{"MessageType":"SubscribeRealtime","Exchange":"NSE","InstrumentIdentifier":"BAJAJ-AUTO"}
{"MessageType":"SubscribeRealtime","Exchange":"CDS","InstrumentIdentifier":"USDINR-I"}
{"MessageType":"SubscribeRealtime","Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038362,"ServerTime":1594038362,"AverageTradedPrice":136.49,"BuyPrice":136.4,"BuyQty":147,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.4,"LastTradeQty":5,"Open":132.4,"OpenInterest":16857,"QuotationLot":1250.0,"SellPrice":136.5,"SellQty":144,"TotalQtyTraded":121396,"Value":20710982750.0,"PreOpen":false,"PriceChange":4.3,"PriceChangePercentage":3.26,"OpenInterestChange":4126,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038363,"ServerTime":1594038363,"AverageTradedPrice":136.49,"BuyPrice":136.4,"BuyQty":145,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.4,"LastTradeQty":31,"Open":132.4,"OpenInterest":16837,"QuotationLot":1250.0,"SellPrice":136.5,"SellQty":131,"TotalQtyTraded":121427,"Value":20716270000.0,"PreOpen":false,"PriceChange":4.3,"PriceChangePercentage":3.26,"OpenInterestChange":4106,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038364,"ServerTime":1594038364,"AverageTradedPrice":136.49,"BuyPrice":136.4,"BuyQty":142,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.4,"LastTradeQty":1,"Open":132.4,"OpenInterest":16836,"QuotationLot":1250.0,"SellPrice":136.5,"SellQty":118,"TotalQtyTraded":121428,"Value":20716440500.0,"PreOpen":false,"PriceChange":4.3,"PriceChangePercentage":3.26,"OpenInterestChange":4105,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038365,"ServerTime":1594038365,"AverageTradedPrice":136.49,"BuyPrice":136.4,"BuyQty":150,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.4,"LastTradeQty":15,"Open":132.4,"OpenInterest":16845,"QuotationLot":1250.0,"SellPrice":136.5,"SellQty":102,"TotalQtyTraded":121443,"Value":20718999375.0,"PreOpen":false,"PriceChange":4.3,"PriceChangePercentage":3.26,"OpenInterestChange":4114,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038365,"ServerTime":1594038366,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":148,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.5,"LastTradeQty":37,"Open":132.4,"OpenInterest":16842,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":153,"TotalQtyTraded":121480,"Value":20725312875.0,"PreOpen":false,"PriceChange":4.4,"PriceChangePercentage":3.33,"OpenInterestChange":4111,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038366,"ServerTime":1594038366,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":146,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.5,"LastTradeQty":7,"Open":132.4,"OpenInterest":16840,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":152,"TotalQtyTraded":121487,"Value":20726507250.0,"PreOpen":false,"PriceChange":4.4,"PriceChangePercentage":3.33,"OpenInterestChange":4109,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038367,"ServerTime":1594038367,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":141,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.5,"LastTradeQty":11,"Open":132.4,"OpenInterest":16834,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":152,"TotalQtyTraded":121498,"Value":20728384125.0,"PreOpen":false,"PriceChange":4.4,"PriceChangePercentage":3.33,"OpenInterestChange":4103,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038368,"ServerTime":1594038368,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":127,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.5,"LastTradeQty":12,"Open":132.4,"OpenInterest":16832,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":161,"TotalQtyTraded":121510,"Value":20730431750.0,"PreOpen":false,"PriceChange":4.4,"PriceChangePercentage":3.33,"OpenInterestChange":4101,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038368,"ServerTime":1594038369,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":128,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.5,"LastTradeQty":14,"Open":132.4,"OpenInterest":16824,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":161,"TotalQtyTraded":121524,"Value":20732820500.0,"PreOpen":false,"PriceChange":4.4,"PriceChangePercentage":3.33,"OpenInterestChange":4093,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038369,"ServerTime":1594038370,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":111,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.6,"LastTradeQty":30,"Open":132.4,"OpenInterest":16842,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":165,"TotalQtyTraded":121554,"Value":20737940500.0,"PreOpen":false,"PriceChange":4.5,"PriceChangePercentage":3.41,"OpenInterestChange":4111,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038370,"ServerTime":1594038370,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":109,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.6,"LastTradeQty":5,"Open":132.4,"OpenInterest":16840,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":167,"TotalQtyTraded":121559,"Value":20738794000.0,"PreOpen":false,"PriceChange":4.5,"PriceChangePercentage":3.41,"OpenInterestChange":4109,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038371,"ServerTime":1594038371,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":109,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.6,"LastTradeQty":2,"Open":132.4,"OpenInterest":16841,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":165,"TotalQtyTraded":121561,"Value":20739135500.0,"PreOpen":false,"PriceChange":4.5,"PriceChangePercentage":3.41,"OpenInterestChange":4110,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038372,"ServerTime":1594038372,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":109,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.6,"LastTradeQty":15,"Open":132.4,"OpenInterest":16839,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":158,"TotalQtyTraded":121576,"Value":20741696750.0,"PreOpen":false,"PriceChange":4.5,"PriceChangePercentage":3.41,"OpenInterestChange":4108,"MessageType":"RealtimeResult"}
{"Exchange":"MCX","InstrumentIdentifier":"NATURALGAS-I","LastTradeTime":1594038373,"ServerTime":1594038373,"AverageTradedPrice":136.49,"BuyPrice":136.5,"BuyQty":139,"Close":132.1,"High":139.6,"Low":132.0,"LastTradePrice":136.6,"LastTradeQty":3,"Open":132.4,"OpenInterest":16839,"QuotationLot":1250.0,"SellPrice":136.6,"SellQty":113,"TotalQtyTraded":121579,"Value":20742208750.0,"PreOpen":false,"PriceChange":4.5,"PriceChangePercentage":3.41,"OpenInterestChange":4108,"MessageType":"RealtimeResult"}
{"MessageType":"Echo"}

– To know how each function works, you can uncomment required function in main() (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 runs at least 1 symbol from each Exchange Segment. 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?