7

I'm using websocket in my Angular app using rxjs.

I'm instanciating it like this : i want to add some headers :

myheader : "abcde"

how my i do it ?

here is my code:

import { BehaviorSubject, of, Subscription, Subject, Observable, NextObserver } from 'rxjs';

@Injectable()
export class WebsocketService  {

  openConnection() {
    this.close();
    const url = 'wss://echo.websocket.org';
    this.connection$ = webSocket({
      url, 
      openObserver: this.openObserver,
      closeObserver: this.closeObserver,
    });
    this.getmsg();

  }
}

Suggestions ?

nopassport1
  • 1,821
  • 1
  • 25
  • 53
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
  • I'm using `@stomp/ng2-stompjs` to help me with websocket connections. the `rxStompService` has a `configure` which allows for `connectHeaders`. I don't think the ordinary WebSocket API allows for headers. You could pass some query parameters to the url though... `wss://echo.websocket.org?firstvar=test` – Carsten Jan 29 '20 at 13:45
  • Does this answer your question? [HTTP headers in Websockets client API](https://stackoverflow.com/questions/4361173/http-headers-in-websockets-client-api) – Християн Христов Apr 20 '21 at 10:18

1 Answers1

1

There is no way to pass additional headers when creating a WebSocket, neither with RxJS nor with plain JavaScript / DOM API.

You can use Query Params or Cookies to provide additional details to your server.

ggradnig
  • 13,119
  • 2
  • 37
  • 61