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

does not support http headers #172

Open
pinchez254 opened this issue Dec 4, 2021 · 4 comments
Open

does not support http headers #172

pinchez254 opened this issue Dec 4, 2021 · 4 comments

Comments

@pinchez254
Copy link

websocket class support headers out of the box

declare var WebSocket: {
    prototype: WebSocket;
    new (
        uri: string,
        protocols?: string | string[] | null,
        options?: {
            headers: { [headerName: string]: string };
            [optionName: string]: any;
        } | null,
    ): WebSocket;
    readonly CLOSED: number;
    readonly CLOSING: number;
    readonly CONNECTING: number;
    readonly OPEN: number;
};

the options supported here are

export declare type Options = {
    WebSocket?: any;
    maxReconnectionDelay?: number;
    minReconnectionDelay?: number;
    reconnectionDelayGrowFactor?: number;
    minUptime?: number;
    connectionTimeout?: number;
    maxRetries?: number;
    maxEnqueuedMessages?: number;
    startClosed?: boolean;
    debug?: boolean;
};
@johnbonds
Copy link

This should probably be closed. It appears that WebSockets doesn't support adding headers to the request:

https://stackoverflow.com/a/58451279/6365919

Looks like it works in my react-native project but not in the web implementation.

Work around I've used is to add the bearer token as a parameter to the URL and then implement a custom security lookup in the backend based on the presence of the token in the URL. Hope this helps.

ex: wss://myserver.com/sock?t={bearer token}

@sayhicoelho
Copy link

@johnbonds Since we have a headers option in ws lib, so why don't we have the same option here?

Ref: https://github.com/websockets/ws/blob/master/lib/websocket.js#L720

@varunk29
Copy link

varunk29 commented Sep 7, 2022

Web implementation of WebSockets doesn't support headers, it supports Sec-WebSocket-Protocol
refer the below link for more info

Ref :https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api

@geertendoornenbal
Copy link

That is only for the browser implementation of websockets, however the nodejs websocket use case (using the ws lib) does support headers.
A common method is to send the Authorization header along with the upgrade request for normal Auth behavior.

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

5 participants