Websocket basic Authorization in Node.js

Secure websockets, authenticated with Basic http authentication.

At the client-side they throw a popup and you provide it with an username and a password to authenticate yourself and gain access.

At the server-side you have to add this to your request headers.

There are a lot of websocket client packages available on npm, but they don’t have easy access to the request headers.

We extended the nodejs-websocket package with this extra feature.

This way we could add Basic access authentication headers to our project through the extraHeaders property in options.

Example

var ws = require('nodejs-websocket')

var name = 'test'

var password = 'test123'

var options = {

	extraHeaders: {

  // implementation of Basic access authentication 

		Authorization: 'Basic ' + new Buffer(name+ ':' + password).toString('base64')

	}

}

var conn = ws.connect('wss://data.appsaloon.io/socket',options)



conn.on('connect',function(){

    conn.sendText('data')

})