Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'on' of undefined #374

Closed
fliespl opened this issue Dec 26, 2020 · 4 comments
Closed

Cannot read property 'on' of undefined #374

fliespl opened this issue Dec 26, 2020 · 4 comments

Comments

@fliespl
Copy link

fliespl commented Dec 26, 2020

Version: 6.0.1

I am curious if documentation is still valid, cause example from it fails immediately.

const redisAdapter = require('socket.io-redis')({'host': '127.0.0.1', 'port': 6379});

redisAdapter.pubClient.on('error', function(){});
redisAdapter.subClient.on('error', function(){});
redisAdapter.pubClient.on('error', function(){});
                       ^

TypeError: Cannot read property 'on' of undefined
    at Object.<anonymous> (E:\Workspace\web\sf5\socket\server.js:6:24)
@cinout
Copy link

cinout commented Jan 8, 2021

I encountered a similar issue. Have you found a solution to this issue @fliespl ?

I used TS way to create my redisAdapter, the code snippet is here:

import { RedisClient } from "redis";
import { createAdapter } from "socket.io-redis";

const pubClient = new RedisClient({ host:hostName, port: portNum});
const subClient = pubClient.duplicate();

const redisAdapter = createAdapter({
      pubClient,
      subClient,
    });

Then if I do console.log( redisAdapter.pubClient ), the output is undefined.

@fliespl
Copy link
Author

fliespl commented Jan 9, 2021

@cinout yes, I did it like that.

Hooked events on custom defined sub/pubClient without going through redisAdapter.pubClient.

const pubClient = redis.createClient(6379, '127.0.0.1');
const subClient = pubClient.duplicate();

const redisAdapter = require('socket.io-redis').createAdapter({pubClient: pubClient, subClient: subClient})

pubClient.on('error', (err) => {
    console.log(err);
})
subClient.on('error', (err) => {
    console.log(err);
})

@cinout
Copy link

cinout commented Jan 11, 2021

@cinout yes, I did it like that.

Hooked events on custom defined sub/pubClient without going through redisAdapter.pubClient.

const pubClient = redis.createClient(6379, '127.0.0.1');
const subClient = pubClient.duplicate();

const redisAdapter = require('socket.io-redis').createAdapter({pubClient: pubClient, subClient: subClient})

pubClient.on('error', (err) => {
    console.log(err);
})
subClient.on('error', (err) => {
    console.log(err);
})

@fliespl That works. Thank you for the help!

@darrachequesne
Copy link
Member

Arf, that is indeed a change in the latest release that wasn't updated in the documentation, sorry for that.

I think the best way for now is to provide your own redis clients, as suggested above.

darrachequesne added a commit that referenced this issue May 11, 2021
BREAKING CHANGE: the library will no longer create Redis clients on
behalf of the user.

Before:

```js
io.adapter(redisAdapter({ host: "localhost", port: 6379 }));
```

After:

```js
const pubClient = createClient({ host: "localhost", port: 6379 });
const subClient = pubClient.duplicate();

io.adapter(redisAdapter(pubClient, subClient));
```

Related:

- #314
- #374
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants