You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

4 years ago
  1. # socket.io-emitter
  2. [![Build Status](https://travis-ci.org/socketio/socket.io-emitter.svg?branch=master)](https://travis-ci.org/socketio/socket.io-emitter)
  3. [![NPM version](https://badge.fury.io/js/socket.io-emitter.svg)](http://badge.fury.io/js/socket.io-emitter)
  4. `socket.io-emitter` allows you to communicate with socket.io servers
  5. easily from non-socket.io processes.
  6. ## How to use
  7. ```js
  8. var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
  9. setInterval(function(){
  10. io.emit('time', new Date);
  11. }, 5000);
  12. ```
  13. ```js
  14. // Different constructor options.
  15. //1. Initialize with host:port string
  16. var io = require('socket.io-emitter')("localhost:6379")
  17. // 2. Initlize with host, port object.
  18. var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
  19. // 3. Can use other node_redis compatible client eg; ioredis.
  20. var Redis = require("ioredis");
  21. var redis = new Redis();
  22. var io = require('socket.io-emitter')(redis);
  23. // Make the emitter works with redis clustered environment.
  24. var Cluster = new Redis.Cluster([
  25. {
  26. host: "localhost",
  27. port: 6379
  28. },
  29. {
  30. host: "localhost",
  31. port: 6378
  32. },
  33. ]);
  34. var io = require('socket.io-emitter')(Cluster);
  35. ```
  36. ## Examples
  37. ```js
  38. var io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
  39. // sending to all clients
  40. io.emit('broadcast', /* ... */);
  41. // sending to all clients in 'game' room
  42. io.to('game').emit('new-game', /* ... */);
  43. // sending to individual socketid (private message)
  44. io.to(<socketid>).emit('private', /* ... */);
  45. var nsp = io.of('/admin');
  46. // sending to all clients in 'admin' namespace
  47. nsp.emit('namespace', /* ... */);
  48. // sending to all clients in 'admin' namespace and in 'notifications' room
  49. nsp.to('notifications').emit('namespace', /* ... */);
  50. ```
  51. **Note:** acknowledgements are not supported
  52. ## Error handling
  53. Access the `redis` to subscribe to its `error` event:
  54. ```js
  55. var emitter = require('socket.io-emitter')("localhost:6379");
  56. emitter.redis.on('error', onError);
  57. function onError(err){
  58. console.log(err);
  59. }
  60. ```
  61. ## API
  62. ### Emitter(client[, opts])
  63. `client` is a [node_redis](https://github.com/mranney/node_redis)
  64. compatible client that has been initialized with the `return_buffers`
  65. option set to `true`. This argument is optional.
  66. The following options are allowed:
  67. - `key`: the name of the key to pub/sub events on as prefix (`socket.io`)
  68. - `host`: host to connect to redis on (`localhost`)
  69. - `port`: port to connect to redis on (`6379`)
  70. - `socket`: unix domain socket to connect to redis on (`"/tmp/redis.sock"`)
  71. ### Emitter(clientUri[, opts]
  72. Same as above, but `clientUri` is a string of the format `host:port`
  73. to connect to redis to.
  74. ### Emitter(opts)
  75. If you don't want to supply a redis client object, and want
  76. `socket.io-emitter` to intiialize one for you, make sure to supply the
  77. `host` and `port` options.
  78. ### Emitter#to(room:String):Emitter
  79. ### Emitter#in(room:String):Emitter
  80. Specifies a specific `room` that you want to emit to.
  81. ### Emitter#of(namespace:String):Emitter
  82. Specifies a specific namespace that you want to emit to.
  83. ## License
  84. MIT