apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.58k stars 133 forks source link

inside docker graphql-redis-subscriptions searches to connect to localhost #263

Closed PandasProperty closed 1 year ago

PandasProperty commented 1 year ago

When running manually with npm start it connects ok to the remote redis. But inside docker it searches for localhost:

Error: connect ECONNREFUSED 127.0.0.1:6379
2023-05-26 21:43:37     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
2023-05-26 21:43:37   errno: -111,
2023-05-26 21:43:37   code: 'ECONNREFUSED',
2023-05-26 21:43:37   syscall: 'connect',
2023-05-26 21:43:37   address: '127.0.0.1',
2023-05-26 21:43:37   port: 6379
2023-05-26 21:43:37 }

I have the following code:

import { createClient } from 'redis'
import { RedisPubSub } from 'graphql-redis-subscriptions'

const redisClient = createClient({
  url: process.env.REDIS_URL
})

const connectRedis = async () => {
  await redisClient.connect();
};

connectRedis();

redisClient.on('connect', () => {
  console.log('Redis client connected successfully') // this is logged
  PublicationSubscription = new RedisPubSub({
    connection: {
      host: 'redis-something.cloud.redislabs.com',
      username: 'XXX',
      password: 'XXX',
      port: XXX,
      retryStrategy: times => {
        return Math.min(times * 50, 2000);
      }
    }
  })
})

redisClient.on('error', (err) => { console.error(err) })

export default redisClient

export { PublicationSubscription } 
PandasProperty commented 1 year ago

I've replaced my redis with ioredis


...
const redisClient = new Redis({
    host: 'redis-something.cloud.redislabs.com',
    username: 'XXX',
    password: 'XXX',
    port: XXX,
})
...
const options = {
  host: 'YYYY'
  username: 'XXX',
  password: 'XXX',
  port: YYYY,
  retryStrategy: (times: number) => {
    return Math.min(times * 50, 2000);
  }
}
...
redisClient.on('connect', () => {
  console.log('Redis client connected successfully') // this is logged
  PublicationSubscription = new RedisPubSub({
    connection: options,
    publisher: new Redis(options),
    subscriber: new Redis(options)
  })
})

the same. my redisClient gets connected to the right instance and the RedisPubSub searches for localhost

Please help :(

PandasProperty commented 1 year ago

had a lost new RedisPubSub() in another part of the code,I wrote it there when setting up to see the subscriptions all work and forgot about it.