Home > Mobile >  Does Socket IO automatically transmit cookies
Does Socket IO automatically transmit cookies

Time:12-03

I have the following Socket IO Client on my frontend that connects to a service running on Google Cloud Run. I enabled Session Affinity in my Google Cloud Run service to ensure subsequent requests from my Socket IO Client are sent to the same Container instance so I don't get undeliverable events.

With Session Affinity enabled, Google Cloud Run seems to be sending back a session affinity token stored in cookies to identify that client for subsequent requests.

So, I just wanted to confirm if during the time the Socket IO client is connecting, does it also sends across all available cookies as received from Google Cloud Run or do I need to explicitly tell it to do so?

Below is my client code

import io from "socket.io-client";

const socketUrl = EndPoints.SOCKET_IO_BASE;
let socketOptions = { transports: ["websocket"] }
let socket;
if (!socket) {
    socket = io(socketUrl, socketOptions);
    socket.on('connect', () => {
        console.log(`Connected to Server`);
    })
    socket.on('disconnect', () => {
        console.log(`Disconnected from Server`); //This never gets called when the Cloud Run service instance is running, so I can assume a disconnect never happened.
    })
}
export default socket;

CodePudding user response:

By default, the Socket.IO client will automatically include any cookies that it receives from the server in subsequent requests. This means that if the server is sending back a cookie with a session affinity token, the Socket.IO client will automatically include that cookie in its requests to the server.

You don't need to do anything special to make this happen. As long as the server is sending back the cookie with the session affinity token, the Socket.IO client will automatically include that cookie in its requests.

However, if for some reason you don't want the Socket.IO client to include cookies in its requests, you can disable this behavior by setting the withCredentials option to false when you create the Socket.IO client:

let socketOptions = { transports: ["websocket"], withCredentials: false }
  • Related