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

Browserify-compatible #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ Meteor server uses SockJs to implement it's DDP server. With this mode, we can g

> With this mode, `path` option has a special meaning. So, thing twice before using `path` option when you are using this option.

Browser support
====
If your browser supports [WebSockets](http://caniuse.com/#feat=websockets), this DDP Client can be used in the browser too using [Browserify](http://browserify.org/),

Unimplemented Features
====
The node DDP client does not implement ordered collections, something that while in the DDP spec has not been implemented in Meteor yet.
Expand Down
19 changes: 17 additions & 2 deletions lib/ddp-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

var util = require("util");
var events = require("events");
var WebSocket = require("faye-websocket");
var WebSocket;

var isBrowserified = (typeof window !== 'undefined');
if (isBrowserified) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the presence of window.Websocket should be the actual test here. And code should be added to detect when it is being run in a non-Websocket implementing browser and do something more helpful that throwing during package load on the absence of window.Websocket (which is presumably what happens now in that case)

window.WebSocket.prototype.on = function (event, callback) {
this['on' + event] = callback;
};
} else {
var WebSocket = require("faye-websocket");
}

var EJSON = require("ddp-ejson");
var request = require('request');
var pathJoin = require('path').join;
Expand Down Expand Up @@ -402,7 +412,12 @@ DDPClient.prototype._buildWsUrl = function(path) {

DDPClient.prototype._makeWebSocketConnection = function(url) {
var self = this;
self.socket = new WebSocket.Client(url, null, self.tlsOpts);

if (isBrowserified) {
self.socket = new window.WebSocket(url);
} else {
self.socket = new WebSocket.Client(url, null, self.tlsOpts);
}
self._prepareHandlers();
};

Expand Down