@@ -33,32 +33,6 @@ export interface WebSocketEnvironment {
3333}
3434
3535export class WebSocketFactory {
36- /**
37- * Dynamic require that works in both CJS and ESM environments
38- * Bulletproof against strict ESM environments where require might not be in scope
39- * @private
40- */
41- private static dynamicRequire ( moduleId : string ) : any {
42- try {
43- // Check if we're in a Node.js environment first
44- if (
45- typeof process !== 'undefined' &&
46- process . versions &&
47- process . versions . node
48- ) {
49- // In Node.js, both CJS and ESM support require for dynamic imports
50- // Wrap in try/catch to handle strict ESM environments
51- if ( typeof require !== 'undefined' ) {
52- return require ( moduleId )
53- }
54- }
55- return null
56- } catch {
57- // Catches any error from typeof require OR require() call in strict ESM
58- return null
59- }
60- }
61-
6236 private static detectEnvironment ( ) : WebSocketEnvironment {
6337 if ( typeof WebSocket !== 'undefined' ) {
6438 return { type : 'native' , constructor : WebSocket }
@@ -112,39 +86,31 @@ export class WebSocketFactory {
11286 process . versions . node
11387 ) {
11488 const nodeVersion = parseInt ( process . versions . node . split ( '.' ) [ 0 ] )
89+
90+ // Node.js 22+ should have native WebSocket
11591 if ( nodeVersion >= 22 ) {
116- try {
117- if ( typeof globalThis . WebSocket !== 'undefined' ) {
118- return { type : 'native' , constructor : globalThis . WebSocket }
119- }
120- const undici = this . dynamicRequire ( 'undici' )
121- if ( undici && undici . WebSocket ) {
122- return { type : 'native' , constructor : undici . WebSocket }
123- }
124- throw new Error ( 'undici not available' )
125- } catch ( err ) {
126- return {
127- type : 'unsupported' ,
128- error : `Node.js ${ nodeVersion } detected but native WebSocket not found.` ,
129- workaround :
130- 'Install the "ws" package or check your Node.js installation.' ,
131- }
92+ // Check if native WebSocket is available (should be in Node.js 22+)
93+ if ( typeof globalThis . WebSocket !== 'undefined' ) {
94+ return { type : 'native' , constructor : globalThis . WebSocket }
13295 }
133- }
134- try {
135- // Use dynamic require to work in both CJS and ESM environments
136- const ws = this . dynamicRequire ( 'ws' )
137- if ( ws ) {
138- return { type : 'ws' , constructor : ws . WebSocket ?? ws }
139- }
140- throw new Error ( 'ws package not available' )
141- } catch ( err ) {
96+ // If not available, user needs to provide it
14297 return {
14398 type : 'unsupported' ,
144- error : `Node.js ${ nodeVersion } detected without WebSocket support.` ,
145- workaround : 'Install the "ws" package: npm install ws' ,
99+ error : `Node.js ${ nodeVersion } detected but native WebSocket not found.` ,
100+ workaround :
101+ 'Provide a WebSocket implementation via the transport option.' ,
146102 }
147103 }
104+
105+ // Node.js < 22 doesn't have native WebSocket
106+ return {
107+ type : 'unsupported' ,
108+ error : `Node.js ${ nodeVersion } detected without native WebSocket support.` ,
109+ workaround :
110+ 'For Node.js < 22, install "ws" package and provide it via the transport option:\n' +
111+ 'import ws from "ws"\n' +
112+ 'new RealtimeClient(url, { transport: ws })' ,
113+ }
148114 }
149115
150116 return {
0 commit comments