Software /
code /
prosody-modules
Annotate
mod_pubsub_eventsource/mod_pubsub_eventsource.lua @ 5939:db5128d1a16c
mod_report_forward: Fix traceback when reporting a specific message (thanks singpolyma)
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 30 May 2024 17:55:07 +0100 |
parent | 4508:0329cf8cdecb |
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 ""; |
1343
7dbde05b48a9
all the things: Remove trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
1242
diff
changeset
|
31 ""; |
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
|
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]; |
1242
4d1226220e58
mod_pubsub_eventsource: Publish only the content of known payload types (JSON, 'data' [from MQTT])
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
48 local item = event.item; |
4d1226220e58
mod_pubsub_eventsource: Publish only the content of known payload types (JSON, 'data' [from MQTT])
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
49 if (item.name == "json" and item.attr.xmlns == "urn:xmpp:json:0") or (item.name == "data" and item.attr.xmlns == "https://prosody.im/protocol/data") then |
4d1226220e58
mod_pubsub_eventsource: Publish only the content of known payload types (JSON, 'data' [from MQTT])
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
50 item = item[1]; |
4d1226220e58
mod_pubsub_eventsource: Publish only the content of known payload types (JSON, 'data' [from MQTT])
Matthew Wild <mwild1@gmail.com>
parents:
1241
diff
changeset
|
51 end |
4508
0329cf8cdecb
mod_pubsub_eventsource: Fix processing of newlines
Kim Alvefur <zash@zash.se>
parents:
1343
diff
changeset
|
52 local data = "data: "..tostring(item):gsub("\n", "\ndata: ").."\n\n"; |
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
|
53 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
|
54 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
|
55 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
|
56 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
|
57 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
|
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: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
|
60 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
|
61 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
|
62 ["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
|
63 }; |
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
|
64 }); |
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
|
65 |
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
|
66 module:hook_object_event(service.events, "item-published", handle_update); |