UNPKG

6.53 kBTypeScriptView Raw
1// Type definitions for socket.io-redis 1.0.0
2// Project: https://github.com/socketio/socket.io-redis
3// Definitions by: Philipp Holzer <https://github.com/nupplaphil>
4// seeLuck <https://github.com/seeLuck>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
7import SocketIO = require("socket.io");
8
9declare const SocketIORedis: SocketIORedisStatic;
10export = SocketIORedis;
11
12interface SocketIORedisStatic {
13 /**
14 * Default Redis Adapter constructor
15 */
16 (): SocketIORedis.RedisAdapter;
17
18 /**
19 * Creates a new Redis Adapter
20 * @param uri Is a string like localhost:6379 where your redis server is located.
21 * @param opts An optional parameters object
22 */
23 (uri: string, opts?: SocketIORedis.SocketIORedisOptions): SocketIORedis.RedisAdapter;
24
25 /**
26 * Creates a new Redis Adapter
27 * @param opts A parameters object
28 */
29 (opts: SocketIORedis.SocketIORedisOptions): SocketIORedis.RedisAdapter;
30}
31
32/**
33 * TODO: return Value for pubClient and subClient is RedisClient, but the im port throws errors because of "invalid module name in augmenatition"
34 *
35 */
36declare namespace SocketIORedis {
37 /**
38 * Options to pass to the redis server when creating it
39 */
40 interface SocketIORedisOptions {
41
42 /**
43 * The optional name of the key to pub/sub events on as prefix
44 * @default socket.io
45 */
46 key?: string;
47
48 /**
49 * The optional host to connect to redis on
50 * @default localhost
51 */
52 host?: string;
53
54 /**
55 * The optional port to connect to redis on
56 * @default 6379
57 */
58 port?: number;
59
60 /**
61 * The optional password to connect to redis on
62 */
63 auth_pass?: number | string;
64
65 /**
66 * The optional redis client to publish events on
67 */
68 pubClient?: any;
69
70 /**
71 * The optional redis client to subscribe to events on
72 */
73 subClient?: any;
74 }
75
76 interface RedisAdapter extends SocketIO.Adapter {
77
78 /**
79 * This servers key
80 */
81 uid: string;
82
83 /**
84 * The prefix of pub/sub events
85 */
86 prefix: string;
87
88 /**
89 * Optional, the redis client to publish events on
90 */
91 pubClient?: any;
92
93 /**
94 * Optional, the redis client to subscribe to events on
95 */
96 subClient?: any;
97
98 /**
99 * Broadcasts a packet
100 * @param packet The packet to broadcast
101 * @param opts Any options to send along:
102 * - rooms: An optional list of rooms to broadcast to. If empty, the packet is broadcast to all sockets
103 * - except: A list of Socket IDs to exclude
104 * - flags: Any flags that we want to send along ('json', 'volatile', 'broadcast')
105 * @param remote The optional flag, whether the packet came from another node
106 */
107 broadcast: {
108 (packet: any, opts: { rooms?: string[]; except?: string[]; flags?: {[flag: string]: boolean} }): void;
109 (packet: any, opts: { rooms?: string[]; except?: string[]; flags?: {[flag: string]: boolean} }, remote?: boolean): void;
110 };
111
112 /**
113 * Removes a socket from all the rooms that it's joined
114 * @param id The ID of the socket that we're removing
115 * @param callback An optional callback to call when the socket has been
116 */
117 delAll: {
118 (id: string): void;
119 (id: string, callback?: (err?: any) => void): void;
120 };
121
122 /**
123 * clients returns the list of client IDs connected to rooms across all nodes.
124 * @param {string[]} rooms
125 * @param {(err?: any, clients: string[]) => void} callback
126 */
127 clients(rooms: string[], callback: (err: any, clients: string[]) => void) : void;
128
129 /**
130 * clients returns the list of client IDs connected to rooms across all nodes.
131 * @param {(err?: any, clients: string[]) => void} callback
132 */
133 clients(callback: (err: any, clients: string[]) => void) : void;
134
135 /**
136 * clientRooms returns the list of rooms the client with the given ID has joined
137 * (even on another node).
138 * @param {string} id
139 * @param {(err: any, rooms: string[]) => void} callback
140 */
141 clientRooms(id: string, callback: (err: any, rooms: string[]) => void) : void;
142
143 /**
144 * allRooms returns the list of all rooms.
145 * @param {(err: any, rooms: string[]) => void} callback
146 */
147 allRooms(callback: (err: any, rooms: string[]) => void) : void;
148
149 /**
150 * remoteJoin rakes the socket with the given id join the room.
151 * The callback will be called once the socket has joined the room, or with an
152 * err argument if the socket was not found.
153 * @param {string} id the socket Id.
154 * @param {string} room the room Id.
155 * @param {(err: any) => void} callback
156 */
157 remoteJoin(id: string, room: string, callback: (err: any) => void) : void;
158
159 /**
160 * remoteLeave makes the socket with the given id leave the room.
161 * The callback will be called once the socket has left the room, or with an
162 * err argument if the socket was not found.
163 * @param {string} id the socket Id.
164 * @param {string} room the room Id.
165 * @param {(err: any) => void} callback
166 */
167 remoteLeave(id: string, room: string, callback: (err: any) => void) : void;
168
169 /**
170 * remoteDisconnect makes the socket with the given id to get disconnected.
171 * If close is set to true, it also closes the underlying socket.
172 * The callback will be called once the socket was disconnected, or with an
173 * err argument if the socket was not found.
174 * @param {string} id the socket Id.
175 * @param {boolean} close close the underlying socket
176 * @param {(err: any) => void} callback
177 */
178 remoteDisconnect(id: string, close: boolean, callback: (err: any) => void) : void;
179
180 /**
181 * customRequest sends a request to every nodes, that will respond through the
182 * customHook method.
183 * @param {any} data
184 * @param {(err: any, replies: any[]) => void} callback
185 */
186 customRequest(data: any, callback: (err: any, replies: any[]) => void) : void;
187 }
188}