Send custom headers please #267
-
I would love to run graphql-ws on glitch.com, I am following this tutorial of getting websockets running on glitch.com https://youtu.be/eLPhUFHKm0M?t=355 & at the timestamp in the video (5:55) it says that a header of a user-agent is necessary to get websockets to run on glitch.com. I would love a way to send a user-agent header just ike in the video via graphql-ws please! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey there, setting custom headers is not possible in browsers; however, the Passing custom headers with Node's import WebSocket from 'ws'; // yarn add ws
import { createClient } from 'graphql-ws';
class MyWebSocket extends WebSocket {
constructor(address, protocols) {
super(address, protocols, {
headers: {
// your headers go here
'User-Agent': 'graphql-ws client',
},
});
}
}
const client = createClient({
url: 'ws://node.custom-headers:4000/graphql',
webSocketImpl: MyWebSocket,
}); P.S. I added a "Client usage in Node with custom headers" recipe too. |
Beta Was this translation helpful? Give feedback.
Hey there, setting custom headers is not possible in browsers; however, the
User-Agent
header is automatically set there anyway.Passing custom headers with Node's
ws
library is possible though, but not recommended (given the lack of documentation around it). Here's how you can make a WebSocket implementation with custom headers and pass it tographql-ws
: