Jump to content
Sign in to follow this  
szcxdave1988

WebSocket is closed before the connection is established.

Recommended Posts

HI,

I got error: WebSocket is closed before the connection is established. when try to get the live stream data. I download the v5,1,2 for js, but when i open the file it shows version 6.1.4, and the v5 already no supported by lightstreamer.so is the error from the version problem or anything else, please help

Share this post


Link to post

Hi,

We're still using the older Lightstreamer versoin 5 on our backend servers, so it will have incompatibility problems with the v6.1.4 SKD you are using. You can still download the appropriate LS versoin 5.1.2 SDK for the programming language you are using at: https://lightstreamer.com/download/#ls5.

That should help with any incompatibility issues.

Kind Regards, PM

Share this post


Link to post

Hi,

Could you paste in the code you've written where you are logging in/authenticating with lightstreamer and then attempting to subscribe to the live streaming market prices? 

Presumably you receive OK respones for login/authentication? You only receive the websocket error message when you attempt to subscribe to market price streams?

Many thanks, PM

Share this post


Link to post
const url = 'https://ciapi.cityindex.com/TradingAPI/session';
const data = {
'Password': '****',
'AppVersion': '1',
'AppComments': '',
'UserName': '*****',
'AppKey': '****'
};
this.http.post(url, data).subscribe(
(info) => {
const client = new LightstreamerClient('https://pushpreprod.cityindextest9.co.uk/?UserName=****&Session=' + info['Session'],
'PRICES');
client.connect();
const grid = new StaticGrid('hellogrid', true);
const subscription = new Subscription('MERGE', grid.extractItemList(), grid.extractFieldList());
subscription.addListener(grid);
client.subscribe(subscription);
},
(error) => { console.log(error); },
);

Share this post


Link to post

Hi,

I notice that you are attempting to connect to the Lightstreamer on our preproduction environment URL, whereas your login and credentials are from our LIVE environment. Credentials are not compatible between the 2 environments.

Please change the Lightstreamer URL to the Live environment at: https://push.cityindex.com/ and hopefully this will resolve the issue. 

Kind Regards, PM

Share this post


Link to post

THanks, I have changed the url, but shows CORS errors:

Access to XMLHttpRequest at 'https://push.cityindex.com/?UserName=****&Session=*********/lightstreamer/create_session.js' from origin 'http://localhost:4000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
 

Share this post


Link to post

Hi,

I checked and verified that CORS is enabled on our servers by using CURL to simulate a preflight request with an origin of "http://example.com" and received back the expected Access-Control-Allow-OriginAccess-Control-Allow-Methods, and Access-Control-Allow-Headers. 

A similar issue that occurred in the past with another client is described in: http://faq.labs.gaincapital.com/topic/1034-cross-origin-resource-sharing-cors/.

It's possible your POST header has a similar typing / syntax error when specifying contentType: 'application/json'?

Kind Regards, PM

Share this post


Link to post

Thanks, but it' s not work. I change to put the username and session in header, and get errors different, I think almost there: 

Access to XMLHttpRequest at 'https://push.cityindex.com/lightstreamer/create_session.js' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field headers is not allowed by Access-Control-Allow-Headers in preflight response.

 

this.http.post(url, data).subscribe(
(info) => {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
'Session': *****,
'UserName': ******
});
const options = { headers: headers };
const client = new LightstreamerClient('https://push.cityindex.com/', 'PRICES');
client.connectionOptions.setHttpExtraHeaders(options);
client.connect();
const grid = new StaticGrid('hellogrid', true);
const subscription = new Subscription('MERGE', grid.extractItemList(), grid.extractFieldList());
subscription.addListener(grid);
client.subscribe(subscription);
},
(error) => { console.log(error); },
);

Share this post


Link to post

thanks, I solved the problem by add auth info this way. client.connectionDetails.setPassword(***); client.connectionDetails.setUser(***);

but error again with: WebSocket connection to 'wss://push.cityindex.com/lightstreamer' failed: WebSocket is closed before the connection is established.

Share this post


Link to post

Hi,

I did some searching on the Lightstreamer forum and I found a post from one of their staff:

Quote

At present, only the JavaScript client libraries (for both web browsers and Node) use WebSockets, in addition to HTTP, while the other libraries use HTTP only.

 

Which client library are you using? A likely reason for the failure message you are receiving is because the LS client library you are using is one that does not support WebSockets but HTTP only.

Kind Regards, PM

Share this post


Link to post

Hi, 

According to Lightstreamer:

Quote

The message reported means that a websocket close was called before the connection was even given a chance to be established.
This can happen when the JavaScript library determines that has been too long in an attempt to connect; so it gives up and tries some fallbacks (HTTP streaming, polling, ...).

Please could you confirm that despite the error message then the connection eventually succeeds?
The most likely reason for the failure of websocket is that between the server and the client there is some intermediary (such as a proxy) that does not support WS protocol, and prevent the client request to reach the Lightstreamer server.

 

It's possible you may have more success by using HTTP instead of WebSockets.

Kind Regards, PM

Share this post


Link to post

Hi,

Subscribe to the price stream of the market of interest in a similar manner to when using websockets.

Some rough skeleton code is provided below based on the LS documentation - you can find the Lightstreamer documentation in a link next to the development kit you are using from the Lightstreamer site at: https://lightstreamer.com/download/#ls60

var ls = require("lightstreamer-client");

var lsClient = new ls.LightstreamerClient("https://push.cityindex.com", "STREAMINGALL");
lsClient.connectionDetails.setUser("xxxxx");
lsClient.connectionDetails.setPassword("xxxxx"); 
lsClient.connect();

marketId = "xxxxx";

var testSubscription = new ls.Subscription("MERGE", ["PRICE." + marketId], ["MarketId", "TickDate", "Bid"]);
testSubscription.setDataAdapter("PRICES");
testSubscription.setRequestedSnapshot("yes");
lsClient.subscribe(testSubscription);

lsClient.addListener({
    onStatusChange: function (newStatus) {
        console.log(newStatus);
    },

    onServerError: function (errorCode, errorMessage) {
        console.log(errorCode);
        console.log(errorMessage);
    }

});

 

Kind Regards, PM

Share this post


Link to post

Hello,

Due to the amount of price history we store for markets, the hard limit of 4000 is enough to retrieve the entirety of the data for a market. 

As an example, we have GBP/USD daily data going back to 2005, which gives an approximate count of 14 years x 260 bars in a year = 3660 data points.

Kind Regards, PM

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×