socketio / engine.io-client

The engine used in the Socket.IO JavaScript client, which manages the low-level transports such as HTTP long-polling, WebSocket and WebTransport.

Home Page:https://socket.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting response headers while polling

yovanoc opened this issue · comments

Hello, I need to get the response headers while polling.

My use case is that I'm in nodejs and I need to get the set-cookie headers while http requests to use it later.

I actually don't find how, I use Primus with engine.io as a transport, so if it exists can you explain me or could this be added.

Thanks

Hi! There is no official API per se, but you should be able to get it with:

socket.on('open', () => {
  const xhr = socket.transport.pollXhr.xhr; // the XMLHttpRequest object of the initial request
  const cookie = xhr.getResponseHeader('set-cookie');
});

Hello, I tried but I can't find to access that. I'm with primus, the closest I found is this

image

TypeError: Cannot read property 'xhr' of undefined

Here's the log of the transport:

image

Big thanks!

With primus@8.0.1 and engine.io@4.0.5:

const Primus = require("primus");
const httpServer = require("http").createServer();

const primus = new Primus(httpServer, {
  transformer: "engine.io",
  transport: {
    cookie: {
      name: "io",
      httpOnly: true,
      maxAge: 60 * 60 * 24 * 7
    }
  }
});

httpServer.listen(3000);

const Socket = primus.Socket;
const client = new Socket("http://localhost:3000");

client.on("open", () => {
  const xhr = client.socket.transport.pollXhr.xhr;
  console.log(xhr.getResponseHeader("set-cookie")); // prints [ 'io=LaGKykqRnyAKwYjPAAAA; Max-Age=604800; Path=/; HttpOnly; SameSite=Lax' ]
});

Oh maybe the fact that I use engine.io-client 3.4.4 and Primus 7.2.3, but I can't use another version because I not own the server :/

So, there is no way with this engine.io version?

With a little change around the cookie option (which was updated in Engine.IO v4), I get the same result with engine.io-client@3.4.4 and primus@7.2.3:

const Primus = require("primus");
const httpServer = require("http").createServer();

const primus = new Primus(httpServer, {
  transformer: "engine.io",
  transport: {
    cookie: "woot"
  }
});

httpServer.listen(3000);

const Socket = primus.Socket;
const client = new Socket("http://localhost:3000");

client.on("open", () => {
  const xhr = client.socket.transport.pollXhr.xhr;
  console.log(xhr.getResponseHeader("set-cookie"));
});
$ node index.js 
[ 'woot=ep1R1rtyeN3kbKuFAAAA; Path=/; HttpOnly; SameSite=Strict' ]

@yovanoc could you please check?

I does not own the server, so I just take the primus.js compiled online (https://proxyconnection.touch.dofus.com/primus/primus.js) but as you saw in my first reply there is no pollXhr in my socket.transport :/

Unfortunately, I don't think there is much more we can do on this.

For future readers, please refer to my answer above.

I'm going to close this, please reopen if needed.

@darrachequesne I made it working, but its does not seems to get the set-cookie while POST request, I only get GET ones