Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from dshaw/patch-endpoints

Improved endpoint handling. Fixes #3.
  • Loading branch information...
commit fbb063c3c01b251357af1f62fe2cc4962fbb1472 2 parents 5dec729 + 9c82f78
@dshaw authored
View
66 Readme.md
@@ -1,36 +1,37 @@
# socket.io-announce
-Lightweight infrastructure broadcast for use with Socket.io RedisStore
+
+Lightweight infrastructure broadcast for use with Socket.io RedisStore.
## Install
-```
-npm install socket.io-announce
-```
+ npm install socket.io-announce
## Use anywhere in your stack, independent of other socket.io servers.
-```javascript
-var announce = require('socket.io-announce').createClient();
-announce.emit('status', { msg:'Going down for maintaince in 15 minutes', countdown: 15*60*1000 });
-```
+ var announce = require('socket.io-announce').createClient()
+ announce.emit('status', { msg:'Going down for maintaince in 15 minutes', countdown: 15*60*1000 })
## Emit to all users.
-```javascript
-announce.send('Hello, world!');
-announce.emit('quote', { symbol:'APPL', price: 5000 });
-```
+ announce.send('Hello, world!')
+ announce.emit('quote', { symbol:'APPL', price: 5000 })
+
+## Target socket.io rooms.
-## Target specific rooms or namespaces.
+ announce.in('boardroom').send('Yoyo yo!')
+ announce.in('nodeup').emit('tweet', {
+ id: '130749992326533122',
+ user:'@dshaw',
+ text: 'Keeping things small and tightly focused actually makes solving big problems easier.'
+ })
-```javascript
-announce.in('boardroom').send('Yoyo yo!');`
-announce.in('nodeup').emit('tweet', {
- id: '130749992326533122',
- user:'@dshaw',
- text: 'Keeping things small and tightly focused actually makes solving big problems easier.'
-});
-```
+## Target socket.io namespaces (with rooms too).
+
+ var announce = require('socket.io-announce')({ namespace: '/podcast' })
+ announce.in('nodeup').emit('episode', {
+ url: 'http://www.nodeup.com/twentyfour',
+ name: 'NodeUp twentyfour - a secure show'
+ })
## Sample Apps
@@ -38,4 +39,25 @@ announce.in('nodeup').emit('tweet', {
## License
-* MIT
+(The MIT License)
+
+Copyright (c) 2012 Daniel D. Shaw, http://dshaw.com
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
12 announce.js
@@ -150,8 +150,8 @@ Announce.prototype.__defineGetter__('volatile', function () {
* @api public
*/
-Announce.prototype.to = Announce.prototype.in = function (room) {
- this.flags.room = room;
+Announce.prototype.in = Announce.prototype.to = function (room) {
+ this.flags.endpoint = this.namespace + (room ? '/' + room : '');
return this;
};
@@ -173,11 +173,11 @@ Announce.prototype.setFlags = function () {
*/
Announce.prototype.packet = function (packet) {
- packet.endpoint = this.namespace + (this.flags.room ? '/' + this.flags.room : '');
+ packet.endpoint = this.namespace;
- var volatile = this.flags.volatile
- , exceptions = []
- , packet = parser.encodePacket(packet);
+ var packet = parser.encodePacket(packet)
+ , volatile = this.flags.volatile
+ , exceptions = [];
this.publish('dispatch', this.flags.endpoint, packet, volatile, exceptions);
View
7 package.json
@@ -1,6 +1,6 @@
{
"name": "socket.io-announce",
- "version": "0.1.3",
+ "version": "0.1.4-pre",
"description": "Socket.io multi-process announcement channel",
"keywords": [
"socket.io",
@@ -27,10 +27,11 @@
"socket.io": "~0.9.7"
},
"devDependencies": {
- "tap": "~0.2.5"
+ "socket.io-client": "~0.9.7",
+ "tap": "~0.3.0"
},
"scripts": {
- "test": "node test/announce.test.js"
+ "test": "./node_modules/.bin/tap test/*.js"
},
"engines": {
"node": ">= 0.4.0"
View
31 test/namespace-room.test.js
@@ -0,0 +1,31 @@
+var sia = require('..')
+ , sio = require('socket.io')
+ , client = require('socket.io-client')
+ , tap = require('tap')
+ , test = tap.test
+ , port = 15000
+
+test('Announce namespace rooms', function (t) {
+ t.plan(1)
+ port++;
+
+ var announce = sia({ namespace: '/news' })
+ , io = sio.listen(port, { store: new sio.RedisStore })
+ , cl = client.connect('http://localhost:'+port+'/news')
+
+ var news = io.of('/news').on('connection', function (socket) {
+ socket.join('news-room')
+ this.in('news-room').emit('ann2', { everyone: 'in', '/chat': 'will get' });
+ });
+
+ cl.on('connect', function () {
+ announce.in('news-room').emit('ann', 'dshaw')
+ })
+
+ cl.on('ann', function (data) {
+ t.equal(data, 'dshaw', 'news ann received in news-room')
+ cl.disconnect()
+ io.server.close()
+ t.end()
+ })
+})
View
28 test/namespace.test.js
@@ -0,0 +1,28 @@
+var sia = require('..')
+ , sio = require('socket.io')
+ , client = require('socket.io-client')
+ , tap = require('tap')
+ , test = tap.test
+ , port = 15100
+
+test('Announce namespace', function (t) {
+ t.plan(1)
+ port++;
+
+ var announce = sia({ namespace: '/news' })
+ , io = sio.listen(port, { store: new sio.RedisStore })
+ , cl = client.connect('http://localhost:'+port+'/news')
+
+ var news = io.of('/news').on('connection', function (socket) {});
+
+ cl.on('connect', function () {
+ announce.emit('ann', 'dshaw')
+ })
+
+ cl.on('ann', function (data) {
+ t.equal(data, 'dshaw', 'news ann received in news-room')
+ cl.disconnect()
+ io.server.close()
+ t.end()
+ })
+})
View
30 test/room.test.js
@@ -0,0 +1,30 @@
+var sia = require('..')
+ , sio = require('socket.io')
+ , client = require('socket.io-client')
+ , tap = require('tap')
+ , test = tap.test
+ , port = 15200
+
+test('Announce rooms', function (t) {
+ t.plan(1)
+ port++;
+
+ var announce = sia()
+ , io = sio.listen(port, { store: new sio.RedisStore })
+ , cl = client.connect('http://localhost:'+port)
+
+ io.on('connection', function (socket) {
+ socket.join('ann-room')
+ })
+
+ cl.on('connect', function () {
+ announce.in('ann-room').emit('ann', 'dshaw')
+ })
+
+ cl.on('ann', function (data) {
+ t.equal(data, 'dshaw', 'ann received dshaw')
+ cl.disconnect()
+ io.server.close()
+ t.end()
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.