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

Add redisPrefix Option for Laravel Echo Socket.io Redis Connection
 #384

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion src/connector/connector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Channel, PresenceChannel } from './../channel';
import { Channel, PresenceChannel } from '../channel';

export abstract class Connector {
/**
Expand Down
32 changes: 17 additions & 15 deletions src/connector/socketio-connector.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Connector } from './connector';
import { SocketIoChannel, SocketIoPrivateChannel, SocketIoPresenceChannel } from './../channel';
import { SocketIoChannel, SocketIoPrivateChannel, SocketIoPresenceChannel } from '../channel';

/**
* This class creates a connnector to a Socket.io server.
* This class creates a connector to a Socket.io server.
*/
export class SocketIoConnector extends Connector {
/**
Expand All @@ -11,10 +11,12 @@ export class SocketIoConnector extends Connector {
socket: any;

/**
* All of the subscribed channel names.
* All the subscribed channel names.
*/
channels: { [name: string]: SocketIoChannel } = {};

redisPrefix = this.options.redisPrefix ?? '';

/**
* Create a fresh Socket.io connection.
*/
Expand Down Expand Up @@ -58,10 +60,10 @@ export class SocketIoConnector extends Connector {
* Get a channel instance by name.
*/
channel(name: string): SocketIoChannel {
name = this.redisPrefix + name;
if (!this.channels[name]) {
this.channels[name] = new SocketIoChannel(this.socket, name, this.options);
}

return this.channels[name];
}

Expand All @@ -70,32 +72,32 @@ export class SocketIoConnector extends Connector {
*/
privateChannel(name: string): SocketIoPrivateChannel {
if (!this.channels['private-' + name]) {
this.channels['private-' + name] = new SocketIoPrivateChannel(this.socket, 'private-' + name, this.options);
name = this.redisPrefix + 'private-' + name;
this.channels[name] = new SocketIoPrivateChannel(this.socket, name, this.options);
}

return this.channels['private-' + name] as SocketIoPrivateChannel;
return this.channels[name] as SocketIoPrivateChannel;
}

/**
* Get a presence channel instance by name.
*/
presenceChannel(name: string): SocketIoPresenceChannel {
if (!this.channels['presence-' + name]) {
this.channels['presence-' + name] = new SocketIoPresenceChannel(
this.socket,
'presence-' + name,
this.options
);
name = this.redisPrefix + 'private-' + name;
this.channels[name] = new SocketIoPresenceChannel(this.socket, name, this.options);
}

return this.channels['presence-' + name] as SocketIoPresenceChannel;
return this.channels[name] as SocketIoPresenceChannel;
}

/**
* Leave the given channel, as well as its private and presence variants.
*/
leave(name: string): void {
let channels = [name, 'private-' + name, 'presence-' + name];
let channels = [
this.redisPrefix + name,
this.redisPrefix + 'private-' + name,
this.redisPrefix + 'presence-' + name,
];

channels.forEach((name) => {
this.leaveChannel(name);
Expand Down