Salvador Dali
Salvador Dali

Reputation: 222561

Get a value of a particular cookie in Socket.io

I am using socket.io for real time message exchange in my application (most of the application is not written in node). I am looking for a way to get a value of particular cookie with Socket.io.

The closest I get so far is to use: client.request.headers.cookie which returns me a string: io=Ntp0MJVr3J_ifTKjAAAA; my_sess=c08mk6g0u66ifrjm6madt7535mknb7ur; other=fk1sq140hgj6gmp1vdcimkto71.

I am not interested in all of these values and need only my_sess. Surely I can split and parse the string. It is not that hard and the most straightforward way is something like:

var value = cookieStr.split('my_sess=')[1].split('; ')[0];

But before doing this, I would rather ask if there is a native method to give me cookie value.

Upvotes: 1

Views: 60

Answers (1)

Oleg
Oleg

Reputation: 23277

You can install cookie -- a tiny module for cookie parsing and serialization:

npm install cookie

But if you're using Express framework you can require cookie module without installing it:

var cookie = require('express/node_modules/cookie');

Then use it:

var value = cookie.parse(cookieStr).my_sess;

Upvotes: 1

Related Questions