简体   繁体   中英

Using event-source-polyfill on server side (node.js)

Inorder to support event streams in IE and other mobile browsers i'm attempting to use event-source-polyfill ( https://www.npmjs.com/package/event-source-polyfill ).

Can anyone please provide an example on how it could be used on a node service (or) it is just adding it via npm install and then we should be all set? From the documentation it looks like there are some server side requirements ( https://www.npmjs.com/package/event-source-polyfill#server-side-requirements ) out of which the,

  1. Last-Event-ID is a query parameter that needs to be sent in the api end point something like /api/eventstream?lastEventId=0
  2. 2 KB padding for IE which as mentioned in the example we can have this (response.write(":" + Array(2049).join(" ") + "\\n");) on the node part
  3. You need to send "comment" messages each 15-30 seconds, these messages will be used as heartbeat to detect disconnects (what should be done for this one?)

So after npm install event-source-polyfill and then completing the steps from 1,2 and 3 it would eventually work in IE is that a fair understanding.

您还需要将脚本添加到HTML:

 <script src="eventsource.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM