Skip to content

Allow configurable timeout on Connection #1065

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

Merged
merged 4 commits into from
Jun 11, 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
1 change: 1 addition & 0 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ See [jid package](/packages/jid)
- `resource` `<string`> Optional resource for [resource binding](/packages/resource-binding)
- `username` `<string>` Optional username for [sasl](/packages/sasl)
- `password` `<string>` Optional password for [sasl](/packages/sasl)
- `timeout` `<number` Number of miliseconds to wait before timing out (default is `2000`)

Returns an [xmpp](#xmpp) object.

Expand Down
1 change: 1 addition & 0 deletions packages/component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ See [jid package](/packages/jid)
- `service` `<string>` The service to connect to, accepts an URI. eg. `xmpp://localhost:5347`
- `domain` `<string>` Domain of the component. eg. `component.localhost`
- `password` `<string>` Password to use to authenticate with the service.
- `timeout` `<number` Number of miliseconds to wait before timing out (default is `2000`)

Returns an [xmpp](#xmpp) object.

Expand Down
15 changes: 8 additions & 7 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ class Connection extends EventEmitter {

constructor(options = {}) {
super();

if (typeof options === "string") {
options = { domain: options };
}

this.jid = null;
this.timeout = 2000;
this.timeout = options.timeout || 2000;
this.options = options;
this.status = "offline";
this.socket = null;
Expand Down Expand Up @@ -251,11 +256,7 @@ class Connection extends EventEmitter {
async open(options) {
this._status("opening");

if (typeof options === "string") {
options = { domain: options };
}

const { domain, lang, timeout = this.timeout } = options;
const { domain, lang } = options;

const headerElement = this.headerElement();
headerElement.attrs.to = domain;
Expand All @@ -265,7 +266,7 @@ class Connection extends EventEmitter {
this._attachParser(new this.Parser());

await this.write(this.header(headerElement));
return promise(this, "open", "error", timeout);
return promise(this, "open", "error", this.timeout);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/connection/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ test("new Connection()", () => {
expect(conn instanceof EventEmitter).toBe(true);
});

test("new Connection() with custom timeout", () => {
const conn = new Connection({timeout:1234});
expect(conn.jid).toBe(null);
expect(conn.timeout).toBe(1234);
expect(conn instanceof EventEmitter).toBe(true);
});

test("new Connection() with unexpected input", () => {
const conn = new Connection(1234);
expect(conn.jid).toBe(null);
expect(conn.timeout).toBe(2000);
expect(conn instanceof EventEmitter).toBe(true);
});

test("isStanza()", () => {
const conn = new Connection();

Expand Down
Loading