diff --git a/index.js b/index.js index e8180a22c..c491c5a1f 100755 --- a/index.js +++ b/index.js @@ -315,6 +315,24 @@ function create(name, emitter) { } }); + /** + * Access to client-side socket for emitting events + * + * @property sockets + */ + Object.defineProperty(instance, "sockets", { + get: function () { + if (!browserSync.active) { + return { + emit: function () {}, + on: function () {} + }; + } else { + return browserSync.io.sockets; + } + } + }); + instances.push(instance); return instance; diff --git a/test/specs/api/init.sockets.js b/test/specs/api/init.sockets.js new file mode 100644 index 000000000..4589a340c --- /dev/null +++ b/test/specs/api/init.sockets.js @@ -0,0 +1,44 @@ +"use strict"; + +var browserSync = require("../../../"); + +var assert = require("chai").assert; + +describe("API: .sockets", function () { + + it("has access before Browsersync is running via stubs", function (done) { + browserSync.reset(); + var bs = browserSync.create(); + bs.init({ + logLevel: "silent" + }, function (err, bs) { + bs.cleanup(); + done(); + }); + assert.isFunction(bs.sockets.on); + assert.isFunction(bs.sockets.emit); + }); + it("has access after Browsersync is running", function (done) { + browserSync.reset(); + var bs = browserSync.create(); + bs.init({ + logLevel: "silent" + }, function (err, _bs) { + assert.isFunction(bs.sockets.emit); + assert.isFunction(bs.sockets.on); + _bs.cleanup(); + done(); + }); + }); + it("has access before Browsersync is running via main module export + stubs", function (done) { + browserSync.reset(); + var bs = browserSync({ + logLevel: "silent" + }, function (err, bs) { + bs.cleanup(); + done(); + }); + assert.isFunction(bs.sockets.on); + assert.isFunction(bs.sockets.emit); + }); +});