Skip to content

Commit

Permalink
Headers emulation (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Jan 12, 2025
1 parent a48a8a3 commit 4ff290f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/centrifuge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import EventEmitter from 'events';

const defaults: Options = {
headers: {},
token: '',
getToken: null,
data: null,
Expand Down Expand Up @@ -250,6 +251,11 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli
this._token = token;
}

/** setHeaders allows setting connection emulated headers. */
setHeaders(headers: {[key: string]: string}) {
this._config.headers = headers;
}

/** send asynchronous data to a server (without any response from a server
* expected, see rpc method if you need response). */
send(data: any): Promise<void> {
Expand Down Expand Up @@ -1045,6 +1051,9 @@ export class Centrifuge extends (EventEmitter as new () => TypedEventEmitter<Cli
if (this._config.version) {
req.version = this._config.version;
}
if (Object.keys(this._config.headers).length > 0) {
req.headers = this._config.headers;
}

const subs = {};
let hasSubs = false;
Expand Down
9 changes: 9 additions & 0 deletions src/client.proto.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@
"time": {
"type": "int64",
"id": 9
},
"channel": {
"type": "string",
"id": 10
}
},
"reserved": [
Expand Down Expand Up @@ -465,6 +469,11 @@
"version": {
"type": "string",
"id": 5
},
"headers": {
"keyType": "string",
"type": "string",
"id": 6
}
}
},
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ export interface TransportEndpoint {

/** Options for Centrifuge client. */
export interface Options {
// provide header emulation, these headers are sent with first protocol message
// the backend can process those in a customized manner. In case of Centrifugo
// these headers are then used like real HTTP headers sent from the client.
// Requires Centrifugo v6.
headers: {[key: string]: string};
/** allows enabling debug mode */
debug: boolean;
/** allows setting initial connection token (JWT) */
Expand Down

0 comments on commit 4ff290f

Please sign in to comment.