Gathering detailed insights and metrics for tt-event-source-polyfill
Gathering detailed insights and metrics for tt-event-source-polyfill
Gathering detailed insights and metrics for tt-event-source-polyfill
Gathering detailed insights and metrics for tt-event-source-polyfill
EventSource client for Node.js and Browser (polyfill)
npm install tt-event-source-polyfill
Typescript
Module System
Node Version
NPM Version
76.3
Supply Chain
99
Quality
75.2
Maintenance
100
Vulnerability
100
License
JavaScript (99.78%)
HTML (0.22%)
Total Downloads
1,784
Last Day
1
Last Week
3
Last Month
17
Last Year
119
MIT License
3 Stars
207 Commits
9 Watchers
2 Branches
86 Contributors
Updated on May 28, 2025
Latest Version
0.0.12-tt2
Package Id
tt-event-source-polyfill@0.0.12-tt2
Unpacked Size
169.52 kB
Size
48.69 kB
File Count
24
NPM Version
6.4.1
Node Version
8.11.1
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
50%
3
Compared to previous week
Last Month
70%
17
Compared to previous month
Last Year
-27.4%
119
Compared to previous year
3
You can get the code from npm or bower:
npm install event-source-polyfill
bower install event-source-polyfill
Just include src/eventsource.js
or src/eventsource.min.js
in your page to use the polyfill.
Original project: https://github.com/Yaffle/EventSource
Unless a typescript definition file is created for this polyfill, this is how you would use it in an Ionic2 project. It should (in theory) be very similar in an Angular2 project.
npm install event-source-polyfill
Add to (or create) src/app/polyfills.ts (path is relative to where polyfills.ts is) :
import 'path/to/event-source-polyfill/src/eventsource.min.js'
Add anywhere you need access to EventSourcePolyfill class :
declare var EventSourcePolyfill: any;
npm install
) and then run the build (npm run build
). It should generate a new version of src/eventsource.min.js
.http://username:password@github.com
.var es = new EventSourcePolyfill('/events', {
headers: {
'X-Custom-Header': 'value'
}
});
1var PORT = 8081; 2 3var http = require("http"); 4var fs = require("fs"); 5var url = require("url"); 6 7http.createServer(function (request, response) { 8 var parsedURL = url.parse(request.url, true); 9 var pathname = parsedURL.pathname; 10 if (pathname === "/events.php") { 11 12 response.writeHead(200, { 13 "Content-Type": "text/event-stream", 14 "Cache-Control": "no-store", 15 "Access-Control-Allow-Origin": "*" 16 }); 17 18 var padding = new Array(2049); 19 response.write(":" + padding.join(" ") + "\n"); // 2kB padding for IE 20 response.write("retry: 2000\n"); 21 22 var lastEventId = Number(request.headers["last-event-id"]) || Number(parsedURL.query.lastEventId) || 0; 23 24 var timeoutId = 0; 25 var i = lastEventId; 26 var c = i + 100; 27 var f = function () { 28 if (++i < c) { 29 response.write("id: " + i + "\n"); 30 response.write("data: " + i + "\n\n"); 31 timeoutId = setTimeout(f, 1000); 32 } else { 33 response.end(); 34 } 35 }; 36 37 f(); 38 39 response.on("close", function () { 40 clearTimeout(timeoutId); 41 }); 42 43 } else { 44 if (pathname === "/") { 45 pathname = "/index.html"; 46 } 47 if (pathname === "/index.html" || pathname === "../src/eventsource.js") { 48 response.writeHead(200, { 49 "Content-Type": pathname === "/index.html" ? "text/html" : "text/javascript" 50 }); 51 response.write(fs.readFileSync(__dirname + pathname)); 52 } 53 response.end(); 54 } 55}).listen(PORT);
1<?php 2 3 header("Content-Type: text/event-stream"); 4 header("Cache-Control: no-store"); 5 header("Access-Control-Allow-Origin: *"); 6 7 $lastEventId = floatval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0); 8 if ($lastEventId == 0) { 9 $lastEventId = floatval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0); 10 } 11 12 echo ":" . str_repeat(" ", 2048) . "\n"; // 2 kB padding for IE 13 echo "retry: 2000\n"; 14 15 // event-stream 16 $i = $lastEventId; 17 $c = $i + 100; 18 while (++$i < $c) { 19 echo "id: " . $i . "\n"; 20 echo "data: " . $i . ";\n\n"; 21 ob_flush(); 22 flush(); 23 sleep(1); 24 } 25 26?>
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8" /> 5 <title>EventSource example</title> 6 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 7 <script src="../src/eventsource.js"></script> 8 <script> 9 var es = new EventSource("events.php"); 10 var listener = function (event) { 11 var div = document.createElement("div"); 12 var type = event.type; 13 div.appendChild(document.createTextNode(type + ": " + (type === "message" ? event.data : es.url))); 14 document.body.appendChild(div); 15 }; 16 es.addEventListener("open", listener); 17 es.addEventListener("message", listener); 18 es.addEventListener("error", listener); 19 </script> 20</head> 21<body> 22</body> 23</html>
The MIT License (MIT)
Copyright (c) 2012 vic99999@yandex.ru
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.