Skip to content

Commit

Permalink
sendMultiple (#876)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp authored Jan 6, 2021
1 parent 5b4849f commit bc8c6f2
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 35 deletions.
4 changes: 4 additions & 0 deletions packages/client-core/lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Client extends Connection {
return this.Transport.prototype.send.call(this, element, ...args);
}

sendMultiple(...args) {
return this.Transport.prototype.sendMultiple.call(this, ...args);
}

_findTransport(service) {
return this.transports.find((Transport) => {
try {
Expand Down
12 changes: 10 additions & 2 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,25 @@ xmpp.send(xml("presence")).catch(console.error);

Returns a promise that resolves once the stanza is serialized and written to the socket or rejects if any of those fails.

You can also pass multiple stanzas. Here is an example sending the same text message to multiple recipients.
### sendMultiple

Sends multiple stanzas.

Here is an example sending the same text message to multiple recipients.

```js
const message = "Hello";
const recipients = ["[email protected]", "[email protected]"];
const stanzas = recipients.map((address) =>
xml("message", { to: address, type: "chat" }, xml("body", null, message)),
);
xmpp.send(...stanzas).catch(console.error);
xmpp.sendMultiple(stanzas).catch(console.error);
```

Returns a promise that resolves once all the stanzas have been sent.

If you need to send a stanza to multiple recipients we recommend using [Extended Stanza Addressing](https://xmpp.org/extensions/xep-0033.html) instead.

### xmpp.reconnect

See [@xmpp/reconnect](/packages/reconnect).
Expand Down
12 changes: 10 additions & 2 deletions packages/component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,25 @@ xmpp.send(xml("presence")).catch(console.error);

Returns a promise that resolves once the stanza is serialized and written to the socket or rejects if any of those fails.

You can also pass multiple stanzas. Here is an example sending the same text message to multiple recipients.
### sendMultiple

Sends multiple stanzas.

Here is an example sending the same text message to multiple recipients.

```js
const message = "Hello";
const recipients = ["[email protected]", "[email protected]"];
const stanzas = recipients.map((address) =>
xml("message", { to: address, type: "chat" }, xml("body", null, message)),
);
xmpp.send(...stanzas).catch(console.error);
xmpp.sendMultiple(stanzas).catch(console.error);
```

Returns a promise that resolves once all the stanzas have been sent.

If you need to send a stanza to multiple recipients we recommend using [Extended Stanza Addressing](https://xmpp.org/extensions/xep-0033.html) instead.

### xmpp.reconnect

See [@xmpp/reconnect](/packages/reconnect).
15 changes: 15 additions & 0 deletions packages/connection-tcp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ const NS_STREAM = "http://etherx.jabber.org/streams";
* Extensible Messaging and Presence Protocol (XMPP): Core http://xmpp.org/rfcs/rfc6120.html
*/
class ConnectionTCP extends Connection {
async sendMultiple(elements) {
let fragment = "";

for (const element of elements) {
element.parent = this.root;
fragment += element.toString();
}

await this.write(fragment);

for (const element of elements) {
this.emit("send", element);
}
}

socketParameters(service) {
const { port, hostname, protocol } = parseURI(service);

Expand Down
19 changes: 19 additions & 0 deletions packages/connection-tcp/test/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const test = require("ava");
const _Connection = require("../../../packages/connection");
const Connection = require("..");
const net = require("net");
const xml = require("@xmpp/xml");

const NS_STREAM = "http://etherx.jabber.org/streams";

Expand Down Expand Up @@ -52,3 +53,21 @@ test("socketParameters()", (t) => {
undefined,
);
});

test("sendMultiple", async (t) => {
t.plan(1);
const conn = new Connection();
conn.root = xml("root");

const foo = xml("foo");
const bar = xml("bar");

conn.socket = {
write(str, fn) {
t.is(str, "<foo/><bar/>");
fn();
},
};

await conn.sendMultiple([foo, bar]);
});
17 changes: 4 additions & 13 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,10 @@ class Connection extends EventEmitter {
return this.open({ domain, lang });
}

async send(...elements) {
let fragment = "";

for (const element of elements) {
element.parent = this.root;
fragment += element.toString();
}

await this.write(fragment);

for (const element of elements) {
this.emit("send", element);
}
async send(element) {
element.parent = this.root;
await this.write(element.toString());
this.emit("send", element);
}

sendReceive(element, timeout = this.timeout) {
Expand Down
19 changes: 1 addition & 18 deletions packages/connection/test/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const xml = require("@xmpp/xml");
const test = require("ava");
const Connection = require("..");

test("single element", (t) => {
test("send", (t) => {
t.plan(3);
const conn = new Connection();
conn.root = xml("root");
Expand All @@ -27,20 +27,3 @@ test("single element", (t) => {
t.is(element, foo);
});
});

test("multiple elements", (t) => {
t.plan(1);
const conn = new Connection();
conn.root = xml("root");

const foo = xml("foo");
const bar = xml("bar");

conn.socket = {
write(str) {
t.is(str, "<foo/><bar/>");
},
};

conn.send(foo, bar);
});
6 changes: 6 additions & 0 deletions packages/websocket/lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class ConnectionWebSocket extends Connection {
return super.send(element, ...args);
}

async sendMultiple(elements) {
for (const element of elements) {
await this.send(element);
}
}

// https://tools.ietf.org/html/rfc7395#section-3.6
footerElement() {
return new xml.Element("close", {
Expand Down
15 changes: 15 additions & 0 deletions packages/websocket/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,18 @@ test("socket close", (t) => {

socket.socket.emit("close", evt);
});

test("sendMultiple", async (t) => {
t.plan(2);
const conn = new ConnectionWebSocket();
conn.root = xml("root");

const foo = xml("foo");
const bar = xml("bar");

conn.send = () => {
t.pass();
};

await conn.sendMultiple([foo, bar]);
});

0 comments on commit bc8c6f2

Please sign in to comment.