Hi All,
This post is regarding using websockets in frontend frameworks like React js , Angular js or any other frontend frameworks and in node js backend.
Using Websockets in Front End
- Use the following websocket link in the frontend websocket connection.
wss://norenapi.thefirstock.com/NorenWSTP/
- For establishing the initial connection in the websocket send the following json object in string format
{"t":"c","uid":"<userId>","actid":"<userId>","susertoken":"<susertoken>","source":"API"}
- 
‘userId’ is the User Id of your Firstock Account 
- 
‘susertoken’ is the token generated in the login API, Refer this link Login | Firstock After sending the initial request you will get the following success response json 
 string
{ "t": "ck", "s": "OK", "uid": "<userId>" }
If the initial connection payload is wrong, you will get the following error response json string
{ "t": "ck", "s": "NOT_OK"}
- Once the connection establishment is successful, you can send the following json object in string format for getting the continuous feeds for NIFTY 50 and BANK NIFTY
{"t":"d","k":"NSE|26000#NSE|26009"}
For Further reference go through this link General guide lines | Firstock
Using Websockets in Backend - Node js
- Login using firstock account credentials
const Firstock = require('thefirstock');
const firstock = new Firstock();
firstock.login(
  {
    userId: "<userId>",
    password: "<password>",
    TOTP: "<TOTP>",
    vendorCode: "<vendorCode>",
    apiKey: "<apiKey>",
  },
  (err, result) => {
    console.log("Error, ", err);
    console.log("Result: ", result);
  }
);
- For getting the vendor code and api key you can login into Firstock to get the details.
- After Logging in use the following code to connect to websockets
//Initializer//
const ws = firstock.initializeWebSocket(2); 
ws.on("open", function open() {
  firstock.getWebSocketDetails((err, result) => {
    if (!err) {
      firstock.initialSendWebSocketDetails(ws, result, () => {
        //Subscribe Feed
        ws.send(firstock.subscribeFeedAcknowledgement("NSE|26000#NSE|26009")); //Sending NIFTY 50 and BANKNIFTY Token
      });
    }
  });
});
ws.on("error", function error(error) {
  console.log(`WebSocket error: ${error}`);
});
ws.on("message", function message(data) {
  const result = firstock.receiveWebSocketDetails(data);
  console.log("message: ", result);
});
Note:
- Send “2” in “firstock.initializeWebSocket(2)” method when you want to use websocket in the frontend and backend simultaneously
For Further reference go through this link General guide lines | Firstock
 
  

