diff --git a/lib/wsp/server-push.js b/lib/wsp/server-push.js index ac3efa4d..e4c77f7e 100644 --- a/lib/wsp/server-push.js +++ b/lib/wsp/server-push.js @@ -42,7 +42,7 @@ function ready (socket, cb) { * @param {Obejct} context - A gun request context. * @param {Object} clients - IDs mapped to socket instances. * @param {Function} cb - Called for each response. - * @return {Object} - The context object. + * @return {undefined} */ function request (context, clients, cb) { Gun.obj.map(clients, function (client) { @@ -61,6 +61,31 @@ function request (context, clients, cb) { }); } +/** + * Pushes a graph update to a collection of clients. + * @param {Object} context - The context object passed by gun. + * @param {Object} clients - An object mapping URLs to clients. + * @param {Function} cb - Invoked on each client response. + * @return {undefined} + */ +function update (context, clients, cb) { + Gun.obj.map(clients, function (client) { + ready(client, function () { + var msg = { + headers: {}, + body: { + '#': Gun.on.ask(cb), + '$': context.put, + }, + }; + + var serialized = JSON.stringify(msg); + + client.send(serialized); + }); + }); +} + /** * Attaches server push middleware to gun. * @param {Gun} gun - The gun instance to attach to. * @param {WebSocket.Server} server - A websocket server instance. @@ -117,6 +142,14 @@ function attach (gun, server) { if (!isUsingServer(context.gun, server)) { return; } + + update(context, pool, function (err, data) { + var ack = { + '!': err || null, + '$': data.$, + }; + Gun.on.ack(context, ack); + }); }); }