Ws: Websocket Client with Request's cookie jar

Created on 2 Jul 2018  路  7Comments  路  Source: websockets/ws

I'm trying to connect one nodejs client to a nodejs server through websockets, but setup is done using http to login to the server. This is so that the Main server can "recognize" the client through its cookies. Here is how my session is stored:
const request = require('request');
var cookieJar = request.jar();
// an http request to login using the cookieJar

Where in the Websocket initialization can I put the cookies in?

Most helpful comment

const ws = new WebSocket('ws://www.host.com/path', {
  headers: {
    Cookie: 'foo=bar; baz=qux'
  }
});

All 7 comments

const WebSocket = require('ws');

const ws = new WebSocket('ws://www.host.com/path', {
perMessageDeflate: false
//cookie: cookieJar
});

Clarification:
Where in this code can I put an option for sending cookies as well?

const ws = new WebSocket('ws://www.host.com/path', {
  headers: {
    Cookie: 'foo=bar; baz=qux'
  }
});

Closing as answered, discussion can continue if needed.

this should be documented

Feel free to make PR with a documentation improvement.

It might be not obvious but it's already documented. https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options

Any other option allowed in http.request() or https.request().

having the option to reuse request's cookieJar would be a nice feature (path,domains,expire,...) ? it would ease the pain of parsing and handling of cookie to build a raw low level header

Was this page helpful?
0 / 5 - 0 ratings

Related issues

NodePing picture NodePing  路  5Comments

bartosz-m picture bartosz-m  路  3Comments

brainkim picture brainkim  路  4Comments

quesurifn picture quesurifn  路  3Comments

nabeelio picture nabeelio  路  5Comments