For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
1 1 1 2

node JS WSS connection timeout

Hi,

I have been using the examples from the github repository:
Websocket-api NODE JS

The issue is that the socket connection does not open, but times out by default. I am using one with authentication and for the sake of simplicity, I have extracted temporary token from POSTMAN, and including that token directly in the new WS object. My code looks like below:

var WebSocket = require('ws')
var ip = require("ip");
var position = ip.address();
var testToken = // manually extracted token from POSTMAN
var appID = // app ID
var WS_URL = 'wss://streaming.trkd.thomsonreuters.com:15000/WebSocket';
console.log("Connecting to WebSocket " + WS_URL + " ...")
_websocket = new WebSocket(WS_URL, "tr_json2", {
    'headers':{'Cookie':"AuthToken=" + testToken + ";AuthPosition=" + position + ";applicationId=" + appID +";"}});
_websocket.onopen = onOpen;
_websocket.onclose = onClose;
_websocket.onerror = onError;


function onOpen(evt){
    console.log("WebSocket successfully connected!");
}


function onClose(evt){
    console.log("WebSocket Closed");
}

function onError(evt){
    console.log("Error");
    console.log(evt)
}


This is basicly a copy paste from the github repository node JS example, only with hardcoded values. If I run the code, it does not trigger any functions below, only the error one roughly 30 seconds after attempting to connect. My error message in TERMINAL :

$ node custom.js
Connecting to WebSocket wss://streaming.trkd.thomsonreuters.com:15000/WebSocket ...
Error
ErrorEvent {
  target:
   WebSocket {
     _events: { open: [Function], close: [Function], error: [Function] },
     _eventsCount: 3,
     _maxListeners: undefined,
     readyState: 2,
     protocol: '',
     _binaryType: 'nodebuffer',
     _closeFrameReceived: false,
     _closeFrameSent: false,
     _closeMessage: '',
     _closeTimer: null,
     _closeCode: 1006,
     _extensions: {},
     _isServer: false,
     _receiver: null,
     _sender: null,
     _socket: null,
     url: 'wss://streaming.trkd.thomsonreuters.com:15000/WebSocket',
     _req: null },
  type: 'error',
  message: 'connect ETIMEDOUT 159.220.24.157:15000',
  error:
   { Error: connect ETIMEDOUT 159.220.24.157:15000
       at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1161:14)
     errno: 'ETIMEDOUT',
     code: 'ETIMEDOUT',
     syscall: 'connect',
     address: '159.220.24.157',
     port: 15000 } }
WebSocket Closed



My code, as I believe, is basicly a copy paste from the github functions in the official repo. Cant think anything where it breaks, or why am I getting refused. I generate a fresh token with POSTMAN, before sending it.

Regards,
Koppany
treprdp-apiwebsocketsrrtostreaming-pricesnode-js
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @k1001

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?

If so please can you click the 'Accept' text next to the appropriate reply. This will guide all community members who have a similar question.

Thanks,

AHS

@k1001

Hi,

Please be informed that a reply has been verified as correct in answering the question, and has been marked as such.

Thanks,

AHS

Upvotes
Accepted
24.7k 54 17 14

Hello @k1001

Firstly, The WS URL for TRKD should be wss://streaming.trkd.thomsonreuters.com/WebSocket/ (without port 15000), not wss://streaming.trkd.thomsonreuters.com:15000/WebSocket/ .

Secondly, the Elektron WebSocket API supports the authentication via a JSON Login request message only. You cannot set TRKD authentication token via HTTP Header like TRKD JSON REST API. You need to set your username, application id and TRKD authentication token in the JSON login request message as following structure:

{  
   "Domain":"Login",
   "ID":1,
   "Key":{  
      "Elements":{  
         "ApplicationId":"your TRKD application id",
         "AuthenticationToken":"Your TKRD Token",
         "Position":"<your position>"
      },
      "NameType":"AuthnToken",
      "Name":"Your TKRD username"
   }
}

I have modified the market_price.js example and tested with my TRKD account. The application works fine.result.zip

I connect to TRKD WebSocket server with this code:

var WS_URL_TRKD = 'wss://streaming.trkd.thomsonreuters.com/WebSocket/';
_websocket = new WebSocket(WS_URL_TRKD, "tr_json2");

Then I set my Login request message with the following code:

function onOpen(evt) {
    console.log("WebSocket successfully connected!");


    var msg = '{"ID":1,"Domain":"Login","Key":{"Name":"","NameType":"AuthnToken","Elements":{"ApplicationId":"","Position":"","AuthenticationToken":""}}}';


    var msgJson = JSON.parse(msg);
    msgJson.Key.Name = user
    msgJson.Key.Elements.ApplicationId = appId
    msgJson.Key.Elements.Position = position
    msgJson.Key.Elements.AuthenticationToken = testToken


    _websocket.send(JSON.stringify(msgJson));
    console.log("SENT:");
    console.log(JSON.stringify(msgJson, null, 2));
}

result.zip (3.2 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Upvotes
22.1k 59 14 21

Seems to be the case of missing ping messages. Can you verify that your app receives ping message from server and responds with a pong message.

icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.