Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set request headers when connect to server? #722

Closed
litgh opened this issue Jul 9, 2014 · 14 comments
Closed

How to set request headers when connect to server? #722

litgh opened this issue Jul 9, 2014 · 14 comments

Comments

@litgh
Copy link

litgh commented Jul 9, 2014

how to set custom request headers when connect socket.io use js client

@litgh litgh changed the title How to set request headers when connection? How to set request headers when connect to server? Jul 9, 2014
@IonicaBizau
Copy link

I also need that... Did you find any solution?

@hayksaakian
Copy link

i am also interested in this.

I've seen how to do it on the Java socket.io-client but not the JS one

@Bizarrus
Copy link

Won't Work with all transport-methods.
If you using XHR/Ajax Polling, it is possible, but not for WebSockets on HTML5.

http://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-apic

I'm searching for a solution to.

@peteruithoven
Copy link

What are you trying to achieve? Can't you use url parameters?

@anacronw
Copy link

anacronw commented Jun 2, 2015

I'm trying to write a test to verify whether websockets can be eavesdropped on before and after authentication middleware.

In order to successfully authenticate, however, I need to send a cookie session along with the initial http upgrade request. It works in the browser, I just want it to work in my node tests as well.

@bastianwegge
Copy link

Is there any progress on this? Or any hint how to get/set the cookie-session / sessionId within socket.io?

@anacronw
Copy link

I actually got it working. The details of the initial http handshake are available at socket.request. There, you'll find the session. For example, using the socket.io middleware:

io.use(function(socket, next){
    if (!socket.request.session){
      console.log('no session in socket')
      socket.disconnect();
    }
    next();
  });

@darrachequesne
Copy link
Member

Since version 2.0.0, you can now set request headers with the extraHeaders option:

const socket = io({
  transportOptions: {
    polling: {
      extraHeaders: {
        'x-clientid': 'abc'
      }
    }
  }
});

@muhammadnasr
Copy link

How can I add url to this example
const socket = io({
transportOptions: {
polling: {
extraHeaders: {
'x-clientid': 'abc'
}
}
}
});

@IonicaBizau
Copy link

IonicaBizau commented Apr 3, 2018

@muhammadnasr Pass the options in the second argument, the first one being the url:

const socket = io("/your-url-here", {
  transportOptions: {
    polling: {
      extraHeaders: {
        'x-clientid': 'abc'
      }
    }
  }
});

@muhammadnasr
Copy link

That's great.

Do you have sample code to set headers for iOS and Android SDK?

Thanks a lot.

@thethtun-mmds
Copy link

Android : socketio/socket.io-client-java#226

@shuliangfu
Copy link

Can the headers be dynamically set?

@darrachequesne
Copy link
Member

For future readers: please see https://socket.io/docs/v3/client-initialization/#extraHeaders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests