Annotate

mod_pubsub_eventsource/mod_pubsub_eventsource.lua @ 1241:2380a5d71448

mod_pubsub_eventsource: Add CORS headers for cross-domain support
author Matthew Wild <mwild1@gmail.com>
date Mon, 02 Dec 2013 03:06:10 +0000
parent 858:28dba9608499
child 1242:4d1226220e58
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";
1241
2380a5d71448 mod_pubsub_eventsource: Add CORS headers for cross-domain support
Matthew Wild <mwild1@gmail.com>
parents: 858
diff changeset
27 "Access-Control-Allow-Origin: *";
2380a5d71448 mod_pubsub_eventsource: Add CORS headers for cross-domain support
Matthew Wild <mwild1@gmail.com>
parents: 858
diff changeset
28 "Access-Control-Allow-Methods: GET";
2380a5d71448 mod_pubsub_eventsource: Add CORS headers for cross-domain support
Matthew Wild <mwild1@gmail.com>
parents: 858
diff changeset
29 "Access-Control-Max-Age: 7200";
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
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 "";
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 }, "\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
33
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 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
35 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
36 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
37 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
38 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
39 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
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 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
42 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
43
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 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54
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 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
56 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
57 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
58 ["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
59 };
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
60 });
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
61
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
62 module:hook_object_event(service.events, "item-published", handle_update);