Skip to content

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

Closed
@marcosc90

Description

@marcosc90

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions