WebSockets vs. Server-Sent events/EventSource

后端 未结 6 838
清歌不尽
清歌不尽 2020-11-22 02:00

Both WebSockets and Server-Sent Events are capable of pushing data to browsers. To me they seem to be competing technologies. What is the difference between them? When would

6条回答
  •  醉酒成梦
    2020-11-22 02:20

    According to caniuse.com:

    • %96 of global users natively support WebSockets
    • %92 of global users natively support Server-sent events

    You can use a client-only polyfill to extend support of SSE to many other browsers. This is less likely with WebSockets. Some EventSource polyfills:

    • EventSource by Remy Sharp with no other library dependencies (IE7+)
    • jQuery.EventSource by Rick Waldron
    • EventSource by Yaffle (replaces native implementation, normalising behaviour across browsers)

    If you need to support all the browsers, consider using a library like web-socket-js, SignalR or socket.io which support multiple transports such as WebSockets, SSE, Forever Frame and AJAX long polling. These often require modifications to the server side as well.

    Learn more about SSE from:

    • HTML5 Rocks article
    • The W3C spec (published version, editor's draft)

    Learn more about WebSockets from:

    • HTML5 Rocks article
    • The W3C spec (published version, editor's draft)

    Other differences:

    • WebSockets supports arbitrary binary data, SSE only uses UTF-8

提交回复
热议问题