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 ?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With