Skip to content
Open
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const ddp = new DDP(options);
ddp.on("connected", () => {
console.log("Connected");
});
ddp.on("failed"), (version) => {
console.log("Connection failed: protocol version supported from " + version)
});

const subId = ddp.sub("mySubscription");
ddp.on("ready", message => {
Expand Down Expand Up @@ -100,6 +103,8 @@ Available options are:
library. On the server you can use whichever library implements the
websocket protocol (e.g. faye-websocket).

- `version` **string** *optional* [default: `"1"`]: the proposed protocol version

- `autoConnect` **boolean** *optional* [default: `true`]: whether to establish
the connection to the server upon instantiation. When `false`, one can
manually establish the connection with the `connect` method.
Expand Down
12 changes: 9 additions & 3 deletions src/ddp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Queue from "./queue";
import Socket from "./socket";
import {contains, uniqueId} from "./utils";

const DDP_VERSION = "1";
const DDP_VERSION_DEFAULT = "1";
const PUBLIC_EVENTS = [
// Subscription messages
"ready", "nosub", "added", "changed", "removed",
Expand All @@ -30,6 +30,7 @@ export default class DDP extends EventEmitter {
this.autoConnect = (options.autoConnect !== false);
this.autoReconnect = (options.autoReconnect !== false);
this.reconnectInterval = options.reconnectInterval || DEFAULT_RECONNECT_INTERVAL;
this.version = options.version || DDP_VERSION_DEFAULT;

this.messageQueue = new Queue(message => {
if (this.status === "connected") {
Expand All @@ -47,8 +48,8 @@ export default class DDP extends EventEmitter {
// to establish the DDP connection
this.socket.send({
msg: "connect",
version: DDP_VERSION,
support: [DDP_VERSION]
version: this.version,
support: [this.version]
});
});

Expand All @@ -70,6 +71,11 @@ export default class DDP extends EventEmitter {
this.status = "connected";
this.messageQueue.process();
this.emit("connected");
} else if (message.msg === "failed") {
// Close the underlaying socket and emit a 'failed' message with the DDP version the server would rather speak
// (see https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md#messages)
this.socket.close();
this.emit("failed", message.version);
} else if (message.msg === "ping") {
// Reply with a `pong` message to prevent the server from
// closing the connection
Expand Down
25 changes: 25 additions & 0 deletions test/unit/ddp.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,31 @@ describe("`DDP` class", () => {
});
});

it("handle `failed` DDP messages", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
const message = {
id: "id",
msg: "failed",
version: "2" //DDP Mock Server version
};
ddp.socket.emit("message:in", message);

expect(ddp.emit).to.have.been.calledWith("failed", message.version);
});

it("close socket on `failed` DDP messages", () => {
const ddp = new DDP(options);
ddp.socket.close = sinon.spy();
ddp.socket.emit("message:in", {
id: "id",
msg: "failed",
version: "2"
});

expect(ddp.socket.close).to.have.callCount(1);
});

it("triggers `messageQueue` processing upon connection", () => {
const ddp = new DDP(options);
ddp.emit = sinon.spy();
Expand Down