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

Headers emulation #299

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading