Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Callback issue on verifyConnection middleware #144

Open
svu-simpaltek opened this issue Oct 17, 2019 · 12 comments
Open

Callback issue on verifyConnection middleware #144

svu-simpaltek opened this issue Oct 17, 2019 · 12 comments

Comments

@svu-simpaltek
Copy link

In my verifyConnection code, I am calling an api, After the api returns, I try to set next(true) but connection is closed. I am testing a node server app to node client app.

my code:

function Worker() {
// Get websocket server
const wss = this.wss;
// Get http/https server
const server = this.server;

wss.setMiddleware('verifyConnection', (info, next) => {
     console.log('verifyConnection') // ex: https://localhost
     //console.log(info.req.headers.authorization) // ex: https://localhost

     var tmp = info.req.headers.authorization.split(' ');   // Split on a space, the original auth looks like  "Basic Y2hhcmxlczoxMjM0NQ==" and we need the 2nd part

    var buf = new Buffer.alloc(476,tmp[1], 'base64'); // create a buffer and tell it the data coming in is base64
    var plain_auth = buf.toString();        // read it back out as a string

    //console.log("Decoded Authorization ", plain_auth);

    // At this point plain_auth = "username:password"

    var creds = plain_auth.split(':');      // split on a ':'
    //var username = creds[0];
    var accessToken = creds[1];

    request.get('https://dev-nexx-domain.simpaltek.com/api/Public/AuthTest/Ping', {
      'auth': {
        'bearer': accessToken
      }
    }, (err, res, body) => {
            //next(true);

    if (err) { return console.log(err); }
        //console.log(body);

        if(body == 'ALIVE'){
            console.log('authenticated!');
            varNext(true);
        } else {
            console.log('connection rejected!');
            next(true);
        }

    });

  }
);

log error:

error: { Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }
disconnected

@goriunov
Copy link
Member

@svu-simpaltek this may be related to this issue #139 which could be in lower level websocket library. Also just to note your verify connection should not take too long to execute otherwise you may get ECONNRESET in cases where you need to run long checks you better create custom verification mechanism which would be after websocket connection established

@svu-simpaltek
Copy link
Author

the https request returns in milliseconds, so I do not think this is an issue.

@goriunov
Copy link
Member

How do you connect from node client, can you share code from there.

@svu-simpaltek
Copy link
Author

attached is the server and client nodejs app. This is what I used to test the user authentication.

let me know if you need anything else.

server-clusterws.zip

@goriunov
Copy link
Member

@svu-simpaltek Thanks for providing example, just run your code on Macbook and everything worked as expected (could be platform related issue what system are you running on?) if that is the case then problem most likely in the windows binding for uws. ClusterWS 3 uses old builds of uws. I am working on newer version which allow to do engine swap ws/cws (alpha version of ClusterWS 4 supports that you can try it out)

@svu-simpaltek
Copy link
Author

I am on windows. Can you provide more clarity on how to use the new alpha version on switching the engine?

@goriunov
Copy link
Member

goriunov commented Oct 23, 2019

You can follow https://clusterws.github.io/guide/server/ (this docs will be change with newer releases) if you search for engine you should be able to find a place which explains how to change it.

@svu-simpaltek
Copy link
Author

got the beta version and set the engine like this:

new ClusterWS({
engine: "ws",
port: 3002, // specify port of the application
worker: Worker // Worker function must be provided
// All other configurations can be found at bottom of the page
}, function() {
console.log('server up and running at %s port 3002');
})

However, I still get the same result:

error: { Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19) code: 'ECONNRESET' }
disconnected

Any other ideas to test?

@goriunov
Copy link
Member

engine option should be under websocketOptions

@goriunov
Copy link
Member

it should look like

new ClusterWS({
port: 3002,
worker: Worker,
websocketOptions: {
   engine: "ws"
}
})

also make sure to install ws module with npm install ws

@svu-simpaltek
Copy link
Author

I got it to work using the alpha version and ws engine. Thanks for your help. Do you have a timeframe in which the alpha version will be officially released?

@goriunov
Copy link
Member

@svu-simpaltek after more research and tests i have started working on improved concept which will require more set up but gives move power to the user and reduces overhead. Also i am doing more benchmark and overall code improvements. This is VERY rough template on how it may look. Of cause more iterations will be made withing next few weeks. Also most likely version 4.0 will be released without ability to scale between multiple machines and will only support scale in single machine (multiple CPU cores). I can not say exact timelines as i am still testing different approaches and deciding between layouts. I think withing next few weeks i should have some more updates regarding this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants