awslabs / amazon-kinesis-video-streams-webrtc-sdk-js

JS SDK for interfacing with the Amazon Kinesis Video Streams Signaling Service.
https://awslabs.github.io/amazon-kinesis-video-streams-webrtc-sdk-js/examples/index.html
Apache License 2.0
284 stars 139 forks source link

Vite - ws does not work in the browser. Browser clients must use the native WebSocket object #306

Open knichwitz opened 3 months ago

knichwitz commented 3 months ago

This change breaks the dev environment of any developer using vite instead of webpack.

https://github.com/awslabs/amazon-kinesis-video-streams-webrtc-sdk-js/blob/de209884fcd388c3fea4937a5b99354ef9519af3/src/SignalingClient.ts#L151

Workaround in vite.config.ts :

define: { "global": "globalThis" }

sirknightj commented 3 months ago

Hi @knichwitz,

From what you're mentioning, global object doesn't exist in vite.js and instead it is renamed to globalThis?

If so, some ideas to resolve:

  1. this.websocket = new (global.WebSocket || globalThis.WebSocket || require('ws'))(signedURL);
  2. this.websocket = new (WebSocket || require('ws'))(signedURL);

Please let us know if these fix the issue you are seeing, and we can get a PR merged in to resolve this.

knichwitz commented 3 months ago

Hi @sirknightj,

thank you for your swift reply!

The second solution will fix the issue with vite.js

this.websocket = new (WebSocket || require('ws'))(signedURL);