UNPKG

995 BJavaScriptView Raw
1var _ = require("underscore");
2
3module.exports = function(io) {
4 var eventQueue = {
5 // Save the last of every event so we can send them to new clients
6 latestEvents: {},
7
8 initialize: function(io) {
9 var that = this;
10
11 io.of("/widgets").on("connection", function(socket) {
12 socket.on("resend", function (data) {
13 if (that.latestEvents[data]) {
14 socket.emit(data, that.latestEvents[data]);
15 }
16 });
17 });
18
19 // broadcast every log received
20 io.of("/log").on("connection", function(socket) {
21 socket.on("log", function (data) {
22 socket.broadcast.emit('client', data);
23 });
24 });
25
26 },
27
28 send: function(id, data) {
29 this.latestEvents[id] = data;
30 io.of("/widgets").emit(id, data); // emit to widget
31 io.of("/log").emit('client', {widgetId : id, data: data}); // emit to logger
32 }
33 };
34 eventQueue.initialize(io);
35 return eventQueue;
36};
\No newline at end of file