1803
|
1 ---
|
|
2 labels: 'Stage-Beta'
|
|
3 summary: Subscribe to pubsub nodes using the HTML5 EventSource API
|
|
4 ...
|
|
5
|
|
6 Introduction
|
|
7 ------------
|
|
8
|
|
9 [Server-Sent Events](https://en.wikipedia.org/wiki/Server-sent_events)
|
|
10 is a simple HTTP/line-based protocol supported in HTML5, making it easy
|
|
11 to receive a stream of "events" in realtime using the Javascript
|
|
12 [EventSource
|
|
13 API](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).
|
|
14
|
|
15 EventSource is supported in [most modern
|
|
16 browsers](http://caniuse.com/#feat=eventsource), and for the remainder
|
|
17 there are 'polyfill' compatibility layers such as
|
|
18 [EventSource.js](https://github.com/remy/polyfills/blob/master/EventSource.js)
|
|
19 and [jquery.eventsource](https://github.com/rwldrn/jquery.eventsource).
|
|
20
|
|
21 Details
|
|
22 -------
|
|
23
|
|
24 Subscribing to a node from Javascript is easy:
|
|
25
|
|
26 var source = new EventSource('http://pubsub.example.org:5280/eventsource/mynode');
|
|
27 source.onmessage = function (event) {
|
|
28 console.log(event.data); // Do whatever you want with the data here
|
|
29 };
|
|
30
|
|
31 ### Cross-domain issues
|
|
32
|
|
33 The same cross-domain restrictions apply to EventSource that apply to
|
|
34 BOSH, and support for CORS is not clearly standardized yet. You may want
|
|
35 to proxy connections through your web server for this reason. See [BOSH:
|
|
36 Cross-domain
|
|
37 issues](https://prosody.im/doc/setting_up_bosh#proxying_requests) for
|
|
38 more information.
|
|
39
|
|
40 Configuration
|
|
41 -------------
|
|
42
|
|
43 There is no special configuration for this module. Simply load it onto a
|
|
44 pubsub component like so:
|
|
45
|
|
46 Component "pubsub.example.org" "pubsub"
|
|
47 modules_enabled = { "pubsub_eventsource" }
|
|
48
|
|
49 As it uses HTTP to serve the event streams, you can use Prosody's
|
|
50 standard [HTTP configuration options](https://prosody.im/doc/http) to
|
|
51 control how/where the streams are served.
|
|
52
|
|
53 **Note about URLs:** It is important to get the event streams from the
|
|
54 correct hostname (that of the pubsub host). An example stream URL is
|
|
55 `http://pubsub.example.org:5280/eventsource/mynode`. If you need to
|
|
56 access the streams using another hostname (e.g. `example.org`) you can
|
|
57 use the `http_host` option under the Component, e.g.
|
|
58 `http_host = "example.org"`. For more information see the ['Virtual
|
|
59 Hosts'](https://prosody.im/doc/http#virtual_hosts) section of our HTTP
|
|
60 documentation.
|
|
61
|
|
62 Compatibility
|
|
63 -------------
|
|
64
|
|
65 ------- --------------
|
|
66 0.9 Works
|
|
67 0.8 Doesn't work
|
|
68 Trunk Works
|
|
69 ------- --------------
|