@@ -85,6 +85,8 @@ function getRedisClient(url = process.env.REDIS_URL) {
8585 isClusterMode = process . env . REDIS_CLUSTER_MODE === "true" ;
8686 }
8787
88+ console . log ( `[Redis] Creating Redis client - Cluster mode: ${ isClusterMode } ` ) ;
89+
8890 const safeUrl = url || "" ;
8991 const parsedUrl = new URL ( safeUrl ) ;
9092 const host = parsedUrl . hostname ;
@@ -93,7 +95,10 @@ function getRedisClient(url = process.env.REDIS_URL) {
9395 const password = parsedUrl . password || "" ;
9496 const tls = safeUrl . startsWith ( "rediss" ) ;
9597
98+ console . log ( `[Redis] Connection details - Host: ${ host } , Port: ${ port } , TLS: ${ tls } , Username: ${ username } ` ) ;
99+
96100 if ( ! isClusterMode ) {
101+ console . log ( "[Redis] Creating standalone Redis client" ) ;
97102 return redis . createClient ( {
98103 socket : { host, port, tls } ,
99104 username,
@@ -102,6 +107,7 @@ function getRedisClient(url = process.env.REDIS_URL) {
102107 } ) ;
103108 }
104109
110+ console . log ( "[Redis] Creating Redis cluster client" ) ;
105111 return redis . createCluster ( {
106112 useReplicas : true ,
107113 rootNodes : [
@@ -122,17 +128,24 @@ const gameManager = {
122128 if ( process . env . NODE_ENV === "development" ) console . log ( `Publishing ${ JSON . stringify ( message ) } on ${ channel } ` ) ;
123129 this . publisher . publish ( channel , JSON . stringify ( message ) ) ;
124130 } ,
125- subscribe : function ( channel ) {
126- this . subscriber . subscribe ( channel , ( message ) => {
127- const data = JSON . parse ( message ) ;
128- if ( process . env . NODE_ENV === "development" ) console . log ( `Event received on ${ channel } :` , data ) ;
129- this . connections . forEach ( ( { res : existingConnection } ) => {
130- const { profileId } = existingConnection . req . query ;
131- if ( data . profileId === profileId ) {
132- existingConnection . write ( `retry: 5000\ndata: ${ JSON . stringify ( data ) } \n\n` ) ;
133- }
131+ subscribe : async function ( channel ) {
132+ try {
133+ console . log ( `[Redis] Attempting to subscribe to channel: ${ channel } ` ) ;
134+ await this . subscriber . subscribe ( channel , ( message ) => {
135+ const data = JSON . parse ( message ) ;
136+ if ( process . env . NODE_ENV === "development" ) console . log ( `Event received on ${ channel } :` , data ) ;
137+ this . connections . forEach ( ( { res : existingConnection } ) => {
138+ const { profileId } = existingConnection . req . query ;
139+ if ( data . profileId === profileId ) {
140+ existingConnection . write ( `retry: 5000\ndata: ${ JSON . stringify ( data ) } \n\n` ) ;
141+ }
142+ } ) ;
134143 } ) ;
135- } ) ;
144+ console . log ( `[Redis] Successfully subscribed to channel: ${ channel } ` ) ;
145+ } catch ( error ) {
146+ console . error ( `[Redis] Failed to subscribe to channel ${ channel } :` , error ) ;
147+ throw error ;
148+ }
136149 } ,
137150 addConn : function ( connection ) {
138151 const { profileId, interactiveNonce } = connection . res . req . query ;
@@ -189,12 +202,26 @@ gameManager.subscriber.on("error", (err) => handleRedisError("sub", err));
189202// Initialize connections and subscription with proper sequencing
190203async function initRedis ( ) {
191204 try {
205+ console . log ( `[Redis] INTERACTIVE_KEY: ${ process . env . INTERACTIVE_KEY } ` ) ;
206+ console . log ( `[Redis] REDIS_URL: ${ process . env . REDIS_URL ? 'SET' : 'NOT SET' } ` ) ;
207+ console . log ( `[Redis] REDIS_CLUSTER_MODE: ${ process . env . REDIS_CLUSTER_MODE } ` ) ;
208+
209+ console . log ( "[Redis] Connecting publisher..." ) ;
192210 await gameManager . publisher . connect ( ) ;
211+ console . log ( "[Redis] Publisher connected successfully" ) ;
212+
213+ console . log ( "[Redis] Connecting subscriber..." ) ;
193214 await gameManager . subscriber . connect ( ) ;
215+ console . log ( "[Redis] Subscriber connected successfully" ) ;
216+
194217 // Subscribe only after connections are established
195- gameManager . subscribe ( `${ process . env . INTERACTIVE_KEY } _RACE` ) ;
218+ console . log ( `[Redis] Subscribing to channel: ${ process . env . INTERACTIVE_KEY } _RACE` ) ;
219+ await gameManager . subscribe ( `${ process . env . INTERACTIVE_KEY } _RACE` ) ;
220+ console . log ( "[Redis] Subscription established" ) ;
196221 } catch ( err ) {
197222 console . error ( "[Redis] Initialization error:" , err ) ;
223+ console . error ( "[Redis] Error details:" , err . message ) ;
224+ console . error ( "[Redis] Stack trace:" , err . stack ) ;
198225 }
199226}
200227
0 commit comments