socketio / socket.io-redis-emitter

The Socket.IO Redis emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
https://socket.io/docs/v4/redis-adapter/
MIT License
722 stars 121 forks source link

Redis should not be a dependency #89

Closed MickL closed 3 years ago

MickL commented 3 years ago

May you add an example how to use with TypeScript? I tried both:

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

and

import * as socketIoEmitter from 'socket.io-emitter';
const io = socketIoEmitter({ host: '127.0.0.1', port: 6379 });

but i get:

ERROR in ./node_modules/socket.io-emitter/node_modules/redis-parser/lib/hiredis.js 3:14-32 Module not found: Error: Can't resolve 'hiredis' in '/Users/mick/git/myproject/node_modules/socket.io-emitter/node_modules/redis-parser/lib'

Btw. should redis be a peerDependency? I already have Redis in my project (v3) and socket.io-emitter imports v2 so I have two instances running.

darrachequesne commented 3 years ago

Hi! I could not reproduce the issue, both examples seem to work.

That's a bit weird though:

But hiredis is not in the dependencies of the redis-parser package...

should redis be a peerDependency?

I think it should even be in the devDependencies list, and let the user provide its own redis client.

const io = require('socket.io-emitter')(redisClient, { /* additional options */});

But this change would need to be well documented..

I already have Redis in my project (v3) and socket.io-emitter imports v2 so I have two instances running.

You should be able to create the emitter instance with a redis client v3 (the example above).

MickL commented 3 years ago

The problem are the dependencies: I already have redis v3 in my project and this package imports redis v2. If I change redis for this package to v3 then it works.

I think redis should at least be a peerDependency

MickL commented 3 years ago

Hey @darrachequesne I see you updated the readme. Can we still remove redis from the dependencies and force the user to insert a client?

darrachequesne commented 3 years ago

Yes, let's do this :+1:

darrachequesne commented 3 years ago

Done in https://github.com/socketio/socket.io-emitter/commit/a70db12877d901dd0f7085def0a91145b7c83163.

You'll now need to explicitly install the redis package (or another compatible package like ioredis), and pass it in the Emitter constructor:

const { Emitter } = require("socket.io-emitter");
const { createClient } = require("redis");

const redisClient = createClient();
const io = new Emitter(redisClient);
MickL commented 3 years ago

Thats nice! I did the same at nats-emitter https://github.com/MickL/socket.io-nats-emitter#readme