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

TypeError: Cannot read property 'encode' of undefined #222

Closed
marcosc90 opened this issue May 14, 2017 · 12 comments
Closed

TypeError: Cannot read property 'encode' of undefined #222

marcosc90 opened this issue May 14, 2017 · 12 comments

Comments

@marcosc90
Copy link

marcosc90 commented May 14, 2017

When using socket.io-redis@5.0.1 I get the following error:

TypeError: Cannot read property 'encode' of undefined
    at Redis.Adapter.broadcast (/var/www/test/node_modules/socket.io-redis/node_modules/socket.io-adapter/index.js:136:15)
    at Redis.broadcast (/var/www/test/node_modules/socket.io-redis/index.js:414:33)
    at Namespace.emit (/var/www/test/node_modules/socket.io/lib/namespace.js:222:18)
    at EventEmitter.emitter.on.data (/var/www/test/private/test.js:121:16)
    at emitOne (events.js:96:13)
    at EventEmitter.emit (events.js:191:7)
    at Test.handleResponse (/var/www/test/private/test.js:286:17)
    at EventRequest.request.on (/var/www/test/private/test.js:198:9)
    at emitOne (events.js:96:13)
    at EventRequest.emit (events.js:191:7)

My code:

const socketio = require('socket.io');
const express = require('express');

const app = express();
const server = require('http').Server(app);

const io = socketio(server);
const redis = require('socket.io-redis');

server.listen(3002);

io.adapter(redis({ host: 'localhost', port: 6379 }));
io.on('connection', (socket) =>{
	const room = 'test';

	socket.join(room);

	socket.on('chat message', () => {
		console.log("message");
		io.to(room).emit('chat message', 'hi');  //This is where the error starts.
	});
});

I fixed it changing this line:

this.encoder = nsp.server.encoder;

with:

this.encoder = nsp.adapter.encoder;

in socket.io-redis/node_modules/socket.io-adapter/index.js

But I really don't think I should be doing that.

Info:

OS: Ubuntu 16.04 64-bit

├─┬ socket.io@1.7.3
│ └── socket.io-adapter@0.5.0
└─┬ socket.io-redis@5.0.1
└── socket.io-adapter@1.1.0

@johnfrades
Copy link

johnfrades commented May 15, 2017

I'm having the same issue

**Update

Holy cow, i tried your solution and it work. How come you know what to change in that index.js. I know the error is pointing to that but how did you come up by changing the "server" to "adapter"?

Cheers

@darrachequesne
Copy link
Member

As noted in the release note, socket.io-redis@5.0.1 is not compatible with socket.io@1.x.

Related: socketio/socket.io-adapter#47

@marcosc90
Copy link
Author

@darrachequesne Thanks, my bad for not reading the release notes.
In any case I would recommend showing a warning if socket.io version is lower than 2.

@olegmdev
Copy link

I have this issue with socket.io@2.0.1.

@marcosc90 's solution helped, but it's not acceptable to make such changes

@darrachequesne
Copy link
Member

darrachequesne commented May 31, 2017

but it's not acceptable to make such changes

@olegmdev May I ask what you do mean?

@olegmdev
Copy link

I meant to make changes inside dependent node_modules sources, nevermind.

Rolling back to socket.io-redis@5.0.0 (from 5.0.1) did the trick.

@felipemarques
Copy link

Iam rolling back to socket.io-redis@5.0.0 likve @olegmdev mentioned, but not work

@felipemarques
Copy link

felipemarques commented Oct 17, 2017

This is my package.json

{
"name": "newpubsub",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "NODE_ENV=production node ./bin/www",
"start:dev": "NODE_ENV=development node ./bin/www"
},
"dependencies": {
"bcrypt": "^1.0.3",
"body-parser": "~1.14.1",
"connect-redis": "^3.0.0",
"cookie-parser": "~1.4.0",
"debug": "~2.2.0",
"ejs": "~2.3.1",
"express": "~4.13.3",
"express-session": "^1.11.1",
"express-useragent": "^1.0.8",
"faker": "^4.1.0",
"hiredis": "^0.4.1",
"lodash": "^4.17.4",
"md5": "^2.2.1",
"moment": "^2.17.1",
"morgan": "~1.6.1",
"mysql": "mysqljs/mysql",
"redis": "^2.1.0",
"request": "^2.81.0",
"serve-favicon": "~2.3.0",
"session.socket.io": "^0.2.0",
"socket.io": "^1.3.5",
"socket.io-emitter": "^3.1.1",
"socket.io-express-session": "^0.1.3",
"socket.io-redis": "5.0.0",
"sticky-session": "^1.0.1",
"uniqid": "^4.1.1"
},
"devDependencies": {
"socket-stress-test": "^0.1.0",
"socket.io-client": "^2.0.3"
}
}

@felipemarques
Copy link

Worked only when i update socket.io to 2.x

@darrachequesne
Copy link
Member

@jonathan-casarrubias
Copy link

jonathan-casarrubias commented Oct 8, 2018

I'm having this problem, I'm using socket.io 2.1.1 and socket.io-redis 5.2.0

any ideas?

@danieldunderfelt
Copy link

I used patch-package to make the change @marcosc90 suggested, and it works. Is this a bug? Can it be fixed?

I'm working on a legacy Sails 0.12 app and it is not feasible to upgrade at this time so this is the solution I will have to go with.

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

7 participants