Annotate

mod_pubsub_eventsource/mod_pubsub_eventsource.lua @ 1106:a53f9989a859

mod_muc_log_http: Have same default for showing presence as mod_muc_log
author Kim Alvefur <zash@zash.se>
date Wed, 10 Jul 2013 13:16:33 +0200
parent 858:28dba9608499
child 1241:2380a5d71448
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
858
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 module:depends("http");
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 module:depends("pubsub");
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 local streams = {};
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 local service = hosts[module.host].modules.pubsub.service;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 function client_closed(response)
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local node = response._eventsource_node;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 module:log("debug", "Destroying client for %q", node);
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 streams[node][response] = nil;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 if next(streams[node]) == nil then
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 streams[node] = nil;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 function serve_stream(event, node)
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 module:log("debug", "Client subscribed to: %s", node);
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local response = event.response;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 response.on_destroy = client_closed;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 response._eventsource_node = node;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 response.conn:write(table.concat({
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 "HTTP/1.1 200 OK";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 "Content-Type: text/event-stream";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 "";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 "";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 }, "\r\n"));
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local clientlist = streams[node];
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 if not clientlist then
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 clientlist = {};
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 streams[node] = clientlist;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 clientlist[response] = response.conn;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 return true;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 function handle_update(event)
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 module:log("debug", "Item published: %q", event.node);
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local node = event.node;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local clientlist = streams[node];
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local data = "data: "..tostring(event.item):gsub("\n", "\ndata: \n").."\n\n";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 if not clientlist then module:log("debug", "No clients for %q", node); return; end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 for response, conn in pairs(clientlist) do
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 conn:write(data);
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 module:provides("http", {
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 name = "eventsource";
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 route = {
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 ["GET /*"] = serve_stream;
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 };
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 });
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
28dba9608499 mod_pubsub_eventsource: An experimental plugin for allowing non-XMPP subscriptions to pubsub nodes over HTML5's server-sent events (SSE/EventSource) API
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 module:hook_object_event(service.events, "item-published", handle_update);