From 25a7d33576c46a2a44a765f181024b073cc76262 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 31 Mar 2016 22:30:33 +0200 Subject: [PATCH 1/5] Initial Commit --- socket.io-redis/socket.io-redis-tests.ts | 1 + socket.io-redis/socket.io-redis.d.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 socket.io-redis/socket.io-redis-tests.ts create mode 100644 socket.io-redis/socket.io-redis.d.ts diff --git a/socket.io-redis/socket.io-redis-tests.ts b/socket.io-redis/socket.io-redis-tests.ts new file mode 100644 index 000000000..a417be85f --- /dev/null +++ b/socket.io-redis/socket.io-redis-tests.ts @@ -0,0 +1 @@ +/// diff --git a/socket.io-redis/socket.io-redis.d.ts b/socket.io-redis/socket.io-redis.d.ts new file mode 100644 index 000000000..38c12b305 --- /dev/null +++ b/socket.io-redis/socket.io-redis.d.ts @@ -0,0 +1,14 @@ +// Type definitions for socket.io-redis 1. +// Project: http://socket.io/ +// Definitions by: PROGRE , Damian Connolly , Florent Poujol +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare var io: SocketIORedisStatic; + +declare module 'socket.io-redis' { + export = io; +} + +interface SocketIORedisStatic { + +} From abbb918134829d7fe3c2d012aa6f058f8a1e32da Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 31 Mar 2016 23:23:49 +0200 Subject: [PATCH 2/5] socket.io-redis - Interfaces ready - still tests to do --- socket.io-redis/socket.io-redis-tests.ts | 35 ++++++++ socket.io-redis/socket.io-redis.d.ts | 107 ++++++++++++++++++++++- 2 files changed, 138 insertions(+), 4 deletions(-) diff --git a/socket.io-redis/socket.io-redis-tests.ts b/socket.io-redis/socket.io-redis-tests.ts index a417be85f..0ab6974c7 100644 --- a/socket.io-redis/socket.io-redis-tests.ts +++ b/socket.io-redis/socket.io-redis-tests.ts @@ -1 +1,36 @@ +/// +/// /// + +import socketIO = require('socket.io'); +import ioRedis = require('socket.io-redis'); + +function testUsingWithNodeHTTPServer() { + var io = socketIO(3000); + var adapter = ioRedis({ host: 'localhost', port: 6379 }); + io.adapter(adapter); +} + +function testRestrictingYourselfToANamespace() { + var io = socketIO.listen(80); + var adapter = ioRedis({ host: 'localhost', port: 6379 }); + io.adapter(adapter); + var chat = io + .of('/chat') + .on('connection', function (socket) { + socket.emit('a message', { + that: 'only' + , '/chat': 'will get' + }); + chat.emit('a message', { + everyone: 'in' + , '/chat': 'will get' + }); + }); + + var news = io + .of('/news') + .on('connection', function (socket) { + socket.emit('item', { news: 'item' }); + }); +} diff --git a/socket.io-redis/socket.io-redis.d.ts b/socket.io-redis/socket.io-redis.d.ts index 38c12b305..fd5713853 100644 --- a/socket.io-redis/socket.io-redis.d.ts +++ b/socket.io-redis/socket.io-redis.d.ts @@ -1,14 +1,113 @@ // Type definitions for socket.io-redis 1. -// Project: http://socket.io/ -// Definitions by: PROGRE , Damian Connolly , Florent Poujol +// Project: https://github.com/socketio/socket.io-redis +// Definitions by: Philipp Holzer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare var io: SocketIORedisStatic; +/// +/// + +import { RedisClient } from "redis"; declare module 'socket.io-redis' { + var io: SocketIORedisStatic; + export = io; } interface SocketIORedisStatic { - + /** + * Creates a new Redis Adapter + * @param uri Is a string like localhost:6379 where your redis server is located. + * @param opts An optional parameters object + */ + (uri: string, opts?: SocketIORedis.SocketIORedisOptions): SocketIORedis.RedisAdapter; + + /** + * Creates a new Redis Adapter + * @param opts A parameters object + */ + (opts: SocketIORedis.SocketIORedisOptions): SocketIORedis.RedisAdapter; +} + +declare namespace SocketIORedis { + /** + * Options to pass to the redis server when creating it + */ + interface SocketIORedisOptions { + + /** + * The optional name of the key to pub/sub events on as prefix + * @default socket.io + */ + key?: string; + + /** + * The optional host to connect to redis on + * @default localhost + */ + host?: string; + + /** + * The optional port to connect to redis on + * @default 6379 + */ + port?: string; + + /** + * The optional redis client to publish events on + */ + pubClient?: RedisClient; + + /** + * The optional redis client to subscribe to events on + */ + subClient?: RedisClient; + } + + interface RedisAdapter extends SocketIO.Adapter { + + /** + * This servers key + */ + uid: string; + + /** + * The prefix of pub/sub events + */ + prefix: string; + + /** + * Optional, the redis client to publish events on + */ + pubClient?: RedisClient; + + /** + * Optional, the redis client to subscribe to events on + */ + subClient?: RedisClient; + + /** + * Broadcasts a packet + * @param packet The packet to broadcast + * @param opts Any options to send along: + * - rooms: An optional list of rooms to broadcast to. If empty, the packet is broadcast to all sockets + * - except: A list of Socket IDs to exclude + * - flags: Any flags that we want to send along ('json', 'volatile', 'broadcast') + * @param remote The optional flag, whether the packet came from another node + */ + broadcast: { + (packet: any, opts: { rooms?: string[]; except?: string[]; flags?: {[flag: string]: boolean} }): void; + (packet: any, opts: { rooms?: string[]; except?: string[]; flags?: {[flag: string]: boolean} }, remote?: boolean): void; + }; + + /** + * Removes a socket from all the rooms that it's joined + * @param id The ID of the socket that we're removing + * @param callback An optional callback to call when the socket has been + */ + delAll: { + ( id: string ): void; + ( id: string, callback?: ( err?: any ) => void ): void; + }; + } } From 73ad2ba6f16a2f175d6031b6d82e5a19d84a4f35 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 31 Mar 2016 23:29:00 +0200 Subject: [PATCH 3/5] - some fixes --- socket.io-redis/socket.io-redis.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/socket.io-redis/socket.io-redis.d.ts b/socket.io-redis/socket.io-redis.d.ts index fd5713853..f40647624 100644 --- a/socket.io-redis/socket.io-redis.d.ts +++ b/socket.io-redis/socket.io-redis.d.ts @@ -1,4 +1,4 @@ -// Type definitions for socket.io-redis 1. +// Type definitions for socket.io-redis 1.0.0 // Project: https://github.com/socketio/socket.io-redis // Definitions by: Philipp Holzer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -6,7 +6,7 @@ /// /// -import { RedisClient } from "redis"; +import { RedisClient } from 'redis'; declare module 'socket.io-redis' { var io: SocketIORedisStatic; From 326b6bbed4b58e217015a0820fe0cee4e554c948 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Thu, 31 Mar 2016 23:33:45 +0200 Subject: [PATCH 4/5] - ReferencePath to socket.io --- socket.io-redis/socket.io-redis.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/socket.io-redis/socket.io-redis.d.ts b/socket.io-redis/socket.io-redis.d.ts index f40647624..658ea2599 100644 --- a/socket.io-redis/socket.io-redis.d.ts +++ b/socket.io-redis/socket.io-redis.d.ts @@ -5,8 +5,7 @@ /// /// - -import { RedisClient } from 'redis'; +/// declare module 'socket.io-redis' { var io: SocketIORedisStatic; @@ -14,6 +13,8 @@ declare module 'socket.io-redis' { export = io; } +import { RedisClient } from 'redis'; + interface SocketIORedisStatic { /** * Creates a new Redis Adapter From f712b237abbcb0cb6ff6861cb23fdadd77856415 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Fri, 1 Apr 2016 06:54:12 +0200 Subject: [PATCH 5/5] Pull Request ready (no errors) --- socket.io-redis/socket.io-redis-tests.ts | 34 +++++++++++++++--------- socket.io-redis/socket.io-redis.d.ts | 34 ++++++++++++++---------- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/socket.io-redis/socket.io-redis-tests.ts b/socket.io-redis/socket.io-redis-tests.ts index 0ab6974c7..2ce2be1bd 100644 --- a/socket.io-redis/socket.io-redis-tests.ts +++ b/socket.io-redis/socket.io-redis-tests.ts @@ -1,20 +1,16 @@ -/// /// +/// /// import socketIO = require('socket.io'); import ioRedis = require('socket.io-redis'); +import redis = require('redis'); function testUsingWithNodeHTTPServer() { var io = socketIO(3000); - var adapter = ioRedis({ host: 'localhost', port: 6379 }); + var adapter = ioRedis("http://localhost"); io.adapter(adapter); -} -function testRestrictingYourselfToANamespace() { - var io = socketIO.listen(80); - var adapter = ioRedis({ host: 'localhost', port: 6379 }); - io.adapter(adapter); var chat = io .of('/chat') .on('connection', function (socket) { @@ -27,10 +23,24 @@ function testRestrictingYourselfToANamespace() { , '/chat': 'will get' }); }); +} - var news = io - .of('/news') - .on('connection', function (socket) { - socket.emit('item', { news: 'item' }); - }); +function testErrorHandling() { + var io = socketIO.listen(80); + var adapter = ioRedis("http://localhost"); + adapter.pubClient.on('error', function () { + console.log('pubClient error'); + }); + adapter.subClient.on('error', function() { + console.log('subClient error'); + }); + io.adapter(adapter); +} + +function testCustomClientAuth() { + var io = socketIO.listen(80); + var pub: redis.RedisClient = redis.createClient(8080, 'localhost', { auth_pass: 'pwd' }); + var sub: redis.RedisClient = redis.createClient(8081, 'localhost', { return_buffers: true, auth_pass: 'pwd' }); + var adapter = ioRedis({ pubClient: pub, subClient: sub }); + io.adapter(adapter); } diff --git a/socket.io-redis/socket.io-redis.d.ts b/socket.io-redis/socket.io-redis.d.ts index 658ea2599..b3eb44e5e 100644 --- a/socket.io-redis/socket.io-redis.d.ts +++ b/socket.io-redis/socket.io-redis.d.ts @@ -3,19 +3,21 @@ // Definitions by: Philipp Holzer // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// /// /// declare module 'socket.io-redis' { - var io: SocketIORedisStatic; + var redis: SocketIORedisStatic; - export = io; + export = redis; } -import { RedisClient } from 'redis'; - interface SocketIORedisStatic { + /** + * Default Redis Adapter constructor + */ + (): SocketIORedis.RedisAdapter; + /** * Creates a new Redis Adapter * @param uri Is a string like localhost:6379 where your redis server is located. @@ -30,6 +32,10 @@ interface SocketIORedisStatic { (opts: SocketIORedis.SocketIORedisOptions): SocketIORedis.RedisAdapter; } +/** + * TODO: return Value for pubClient and subClient is RedisClient, but the im port throws errors because of "invalid module name in augmenatition" + * + */ declare namespace SocketIORedis { /** * Options to pass to the redis server when creating it @@ -57,12 +63,12 @@ declare namespace SocketIORedis { /** * The optional redis client to publish events on */ - pubClient?: RedisClient; + pubClient?: any; /** * The optional redis client to subscribe to events on */ - subClient?: RedisClient; + subClient?: any; } interface RedisAdapter extends SocketIO.Adapter { @@ -80,20 +86,20 @@ declare namespace SocketIORedis { /** * Optional, the redis client to publish events on */ - pubClient?: RedisClient; + pubClient?: any; /** * Optional, the redis client to subscribe to events on */ - subClient?: RedisClient; + subClient?: any; /** * Broadcasts a packet * @param packet The packet to broadcast * @param opts Any options to send along: - * - rooms: An optional list of rooms to broadcast to. If empty, the packet is broadcast to all sockets - * - except: A list of Socket IDs to exclude - * - flags: Any flags that we want to send along ('json', 'volatile', 'broadcast') + * - rooms: An optional list of rooms to broadcast to. If empty, the packet is broadcast to all sockets + * - except: A list of Socket IDs to exclude + * - flags: Any flags that we want to send along ('json', 'volatile', 'broadcast') * @param remote The optional flag, whether the packet came from another node */ broadcast: { @@ -107,8 +113,8 @@ declare namespace SocketIORedis { * @param callback An optional callback to call when the socket has been */ delAll: { - ( id: string ): void; - ( id: string, callback?: ( err?: any ) => void ): void; + (id: string): void; + (id: string, callback?: (err?: any) => void): void; }; } }