|
7 | 7 | [](https://github.com/byjg/jquery-sse/releases/) |
8 | 8 | [](https://www.jsdelivr.com/package/npm/jquery-sse) |
9 | 9 |
|
10 | | -A lightweigth jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill. |
11 | | -This plugin try to use the native EventSource object if it supported by the browser. |
12 | | -If there is no native support the request is made by ajax requests (polling). |
| 10 | +A lightweight jQuery Plugin for Server-Sent Events (SSE) EventSource Polyfill. |
| 11 | +This plugin tries to use the native EventSource object if it's supported by the browser. |
| 12 | +If there is no native support, the request is made by Ajax requests (polling). |
13 | 13 | You do not need to change the server side nor the client side. |
14 | 14 |
|
15 | | -*If you are looking for a SSE Polyfill library without jQuery dependency |
| 15 | +*If you are looking for an SSE Polyfill library without jQuery dependency |
16 | 16 | try [yaj-sse](https://github.com/byjg/yaj-sse). The yaj-sse is a port |
17 | 17 | from version 0.1.4 of jQuery SSE.* |
18 | 18 |
|
19 | | -## Example |
| 19 | +## Quick Start |
20 | 20 |
|
21 | | -Client Side |
| 21 | +### Client Side |
22 | 22 |
|
23 | 23 | ```javascript |
24 | 24 | var sse = $.SSE('http://example.com/sse-server.php', { |
25 | | - onMessage: function(e){ |
26 | | - console.log("Message"); console.log(e); |
| 25 | + onMessage: function(e){ |
| 26 | + console.log("Message"); console.log(e); |
27 | 27 | } |
28 | 28 | }); |
29 | 29 | sse.start(); |
30 | 30 | ``` |
31 | 31 |
|
32 | | -Server Side |
| 32 | +### Server Side |
33 | 33 |
|
34 | 34 | ```php |
35 | 35 | echo "data: My Message\n"; |
36 | 36 | echo "\n"; |
37 | 37 | ``` |
38 | 38 |
|
39 | | -## Dependencies |
| 39 | +## Documentation |
40 | 40 |
|
41 | | -* jQuery |
| 41 | +For comprehensive documentation, please refer to the following guides: |
42 | 42 |
|
43 | | -## Install |
| 43 | +1. **[Getting Started](docs/getting-started.md)** - Installation, dependencies, and quick start guide |
| 44 | +2. **[Usage](docs/usage.md)** - Constructor, methods (start, stop), and basic usage |
| 45 | +3. **[Configuration](docs/configuration.md)** - Event callbacks, options, headers, and custom events |
| 46 | +4. **[Examples](docs/examples.md)** - Code examples for common use cases and running examples locally |
| 47 | +5. **[Known Limitations and Quirks](docs/quirks.md)** - Browser compatibility, Ajax fallback limitations, and debugging tips |
| 48 | +6. **[API Reference](docs/api-reference.md)** - Complete API documentation with all methods and properties |
| 49 | +7. **[References and Resources](docs/references.md)** - External resources, specifications, and related projects |
44 | 50 |
|
45 | | -Just download the repository and point to the jQuery plugin: |
| 51 | +## Features |
46 | 52 |
|
47 | | -```html |
48 | | -<script src="jquery.sse.js" ></script> |
49 | | -``` |
| 53 | +- Automatic detection and use of native EventSource when available |
| 54 | +- Seamless fallback to Ajax polling when EventSource is not supported |
| 55 | +- Support for custom events |
| 56 | +- Custom HTTP headers support (automatically uses Ajax mode) |
| 57 | +- Automatic reconnection with configurable retry intervals |
| 58 | +- Event IDs and Last-Event-ID tracking |
| 59 | +- Simple jQuery-style API |
| 60 | + |
| 61 | +## Installation |
50 | 62 |
|
51 | | -or |
| 63 | +### Direct Download |
52 | 64 |
|
53 | 65 | ```html |
54 | | -<script src="jquery.sse.min.js" ></script> |
| 66 | +<script src="jquery.sse.min.js"></script> |
55 | 67 | ``` |
56 | 68 |
|
57 | | -You can also install using bower: |
| 69 | +### Bower |
58 | 70 |
|
59 | 71 | ```bash |
60 | 72 | bower install jquery-sse |
61 | 73 | ``` |
62 | 74 |
|
63 | | -## Usage: |
64 | | - |
65 | | -### Constructor |
66 | | - |
67 | | -``` |
68 | | -var sse = $.SSE(url, settings); |
69 | | -``` |
70 | | - |
71 | | -* url: URL for the server will be sent the events to this page; |
72 | | -* settings: The events and options for the SSE instance |
73 | | - |
74 | | -### Settings List |
75 | | - |
76 | | -All the options: |
77 | | - |
78 | | -``` |
79 | | -var sseObject = $.SSE('sse-server.php', { |
80 | | - onOpen: function (e) {}, |
81 | | - onEnd: function (e) {}, |
82 | | - onError: function (e) {}, |
83 | | - onMessage: function (e) {}, |
84 | | - options: {}, |
85 | | - headers: {}, |
86 | | - events: {} |
87 | | -}); |
88 | | -``` |
89 | | - |
90 | | -**Event onOpen** |
91 | | - |
92 | | -Fired when the connection is opened the first time; |
93 | | - |
94 | | -```javascript |
95 | | -onOpen: function(e){ |
96 | | - console.log("Open"); console.log(e); |
97 | | -}, |
98 | | -``` |
99 | | - |
100 | | -**Event onEnd** |
101 | | - |
102 | | -Fired when the connection is closed and the client will not listen for the server events; |
103 | | - |
104 | | -```javascript |
105 | | -onEnd: function(e){ |
106 | | - console.log("End"); console.log(e); |
107 | | -}, |
108 | | -``` |
109 | | - |
110 | | -**Event onError** |
111 | | - |
112 | | -Fired when the connection error occurs; |
113 | | - |
114 | | -```javascript |
115 | | -onError: function(e){ |
116 | | - console.log("Could not connect"); |
117 | | -}, |
118 | | -``` |
119 | | - |
120 | | -**Event onMessage** |
121 | | - |
122 | | -Fired when the a message without event is received |
123 | | - |
124 | | -```javascript |
125 | | -onMessage: function(e){ |
126 | | - console.log("Message"); console.log(e); |
127 | | -}, |
128 | | -``` |
129 | | - |
130 | | -**Custom Options** |
131 | | - |
132 | | -Define the options for the SSE instance |
133 | | - |
134 | | -```javascript |
135 | | -options: { |
136 | | - forceAjax: false |
137 | | -}, |
138 | | -``` |
139 | | - |
140 | | -* **forceAjax**: Uses ajax even if the EventSource object is supported natively; |
141 | | - |
142 | | - |
143 | | -**Custom Events** |
144 | | - |
145 | | -Fired when the server set the event and match with the key |
146 | | - |
147 | | -For example, if you have a custom event called `myEvent` you may use the follow code: |
| 75 | +### jsDelivr CDN |
148 | 76 |
|
149 | | -```javascript |
150 | | -events: { |
151 | | - myEvent: function(e) { |
152 | | - console.log('Custom Event'); |
153 | | - console.log(e); |
154 | | - } |
155 | | -} |
156 | | -``` |
157 | | - |
158 | | -Server side: |
159 | | - |
160 | | -```php |
161 | | -echo "event: myEvent\n"; // Must match with events in the HTML. |
162 | | -echo "data: My Message\n"; |
163 | | -echo "\n"; |
164 | | -``` |
165 | | - |
166 | | -**Custom Headers** |
167 | | - |
168 | | -You can send custom headers to the request. |
169 | | - |
170 | | -```javascript |
171 | | -headers: { |
172 | | - 'Authorization': 'Bearer 1a234fd4983d' |
173 | | -} |
174 | | -``` |
175 | | - |
176 | | -Note: As the EventSource does not support send custom headers to the request, |
177 | | -the object will fallback automatically to 'forceAjax=true', even this it is not set. |
178 | | - |
179 | | - |
180 | | -## Methods |
181 | | - |
182 | | -**start** |
183 | | - |
184 | | -Start the EventSource communication |
185 | | - |
186 | | -```javascript |
187 | | -sse.start(); |
188 | | -``` |
189 | | - |
190 | | -**stop** |
191 | | - |
192 | | -Stop the EventSource communication |
193 | | - |
194 | | -```javascript |
195 | | -sse.stop(); |
196 | | -``` |
197 | | - |
198 | | - |
199 | | -## Quirks |
200 | | - |
201 | | -The ajax does not support the streaming as the event source supports. In that case we recommend |
202 | | -create a server without streaming and set the "retry" to determine query frequency; |
203 | | - |
204 | | -Example Server Side: |
205 | | - |
206 | | -```php |
207 | | -echo "retry: 3000\n"; |
208 | | -echo "data: My Message\n"; |
209 | | -echo "\n"; |
210 | | -``` |
211 | | - |
212 | | -## Minify |
213 | | - |
214 | | -``` |
215 | | -apt install uglifyjs |
216 | | -
|
217 | | -uglifyjs --compress 'drop_console,drop_debugger' --mangle -r '$,require,exports,_' -o jquery.sse.min.js jquery.sse.js |
| 77 | +```html |
| 78 | +<script src="https://cdn.jsdelivr.net/npm/jquery-sse@latest/jquery.sse.min.js"></script> |
218 | 79 | ``` |
219 | 80 |
|
220 | | -## Running the examples |
| 81 | +## Browser Support |
221 | 82 |
|
222 | | -Start the webserver: |
| 83 | +- ✅ Chrome 6+ |
| 84 | +- ✅ Firefox 6+ |
| 85 | +- ✅ Safari 5+ |
| 86 | +- ✅ Opera 11+ |
| 87 | +- ✅ Edge 79+ |
| 88 | +- ❌ Internet Explorer (uses Ajax fallback automatically) |
223 | 89 |
|
224 | | -```shell |
225 | | -docker run -it --rm -p 8080:80 -v $PWD:/var/www/html byjg/php:7.4-fpm-nginx |
226 | | -``` |
| 90 | +## Contributing |
227 | 91 |
|
228 | | -Open the browser: |
229 | | -http://localhost:8080/examples/sse-client.html |
| 92 | +Contributions are welcome! Please feel free to submit pull requests or open issues on GitHub. |
230 | 93 |
|
231 | | -## References |
| 94 | +## License |
232 | 95 |
|
233 | | -* http://www.w3.org/TR/2009/WD-eventsource-20091029/ |
234 | | -* https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events |
235 | | -* http://html5doctor.com/server-sent-events/ |
236 | | -* http://www.html5rocks.com/en/tutorials/eventsource/basics/ |
| 96 | +This project is licensed under the MIT License. See the [LICENSE](https://opensource.byjg.com/opensource/licensing.html) file for details. |
237 | 97 |
|
238 | 98 | ---- |
239 | 99 | [Open source ByJG](http://opensource.byjg.com) |
0 commit comments