Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjs Websocket : how to add headers

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 ?

like image 253
firasKoubaa Avatar asked Jan 29 '20 13:01

firasKoubaa


People also ask

Does WebSocket have header?

Longer answer: There is no method in the JavaScript WebSockets API for specifying additional headers for the client/browser to send. The HTTP path ("GET /xyz") and protocol header ("Sec-WebSocket-Protocol") can be specified in the WebSocket constructor.

How do I use RXJS WebSockets?

You can use wss for secure websocket connection. import { webSocket } from "rxjs/"webSocket; const subject = webSocket("ws://localhost:8081"); This way you have a ready to use subject that you should subscribe to in order to establish the connection with your endpoint and start receiving and sending some data.

How does angular connect to WebSockets?

To make a WebSocket connection it is necessary to use the HTTP protocol upgrade mechanism, this allows us to move from an HTTP 1.1 connection to HTTP 2.0 (which allows using server push feature), or as in this case to move from an HTTP 1.1 connection to a WebSocket connection.


Video Answer


1 Answers

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.

like image 53
ggradnig Avatar answered Oct 20 '22 13:10

ggradnig