Comparing version 0.5.2 to 0.5.3
@@ -14,9 +14,15 @@ var MuxDemux = require('mux-demux'), | ||
stream.cid = data.cid; | ||
stream.emit('ready'); | ||
stream.emit('ready', data.meta); | ||
} | ||
} | ||
// if the stream is not a stream, then remap args | ||
// if the room is not an instanceof stream, do a few checks | ||
if (! (roomStream instanceof Stream)) { | ||
throw new Error('If arguments are provided, the first argument must be a stream'); | ||
// check if we have a connect function, if so, the connect | ||
if (typeof roomStream.connect == 'function') { | ||
roomStream = roomStream.connect(); | ||
} | ||
else { | ||
throw new Error('If arguments are provided, the first argument must be a stream'); | ||
} | ||
} | ||
@@ -23,0 +29,0 @@ |
@@ -10,3 +10,3 @@ { | ||
], | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"engines": { | ||
@@ -13,0 +13,0 @@ "node": ">= 0.8.x < 0.9.0" |
# Chat | ||
A chat room crdt document type. | ||
Pure Streaming Chat for Node. This is a simple library which implements the core concepts of chat (rooms and connections) without getting tying chat to any particular transport. | ||
## Memory Usage / Performance Tests | ||
## Getting Started | ||
The current implementation (using MuxDemux streams with a centralized CRDT document) consumed approximately 1.5 Gb of memory and was servicing ~560K client streams prior to the test being terminated. A previous implementation using CRDT documents for both the room and the client implementation hit a similar memory usage figure with only 1K client instances. | ||
The first thing you are going to want to do is create a room: | ||
```js | ||
var chat = require('chat'), | ||
room = chat.room(); | ||
``` | ||
Once you have a room, you can then start connecting clients to the room: | ||
```js | ||
var client = chat.client(room); | ||
``` | ||
At this point, we should probably start listening for messages in the room: | ||
```js | ||
room.on('message', function(msg) { | ||
if (msg.data) { | ||
console.log(msg.data); | ||
} | ||
}); | ||
``` | ||
The code above looks specifically for just the data messages that are captured by the room. In addition to the data messages, control messages are also sent which can be captured by examining the `type` property of the message. | ||
At this point, it's probably worth attempting to communicate with the room. This is done using the `write` method of the client: | ||
```js | ||
client.write('hello'); | ||
``` | ||
Strange, we didn't capture any output. This is because as far as the room is concerned, an unknown client is attempting to send messages and this isn't permitted. While the chat package doesn't perform any authentication it does assume that a package using chat (such as [iceman](https://github.com/DamonOehlman/iceman)) will. So at a base level, it requires identification: | ||
```js | ||
client.identify({ nick: 'Bill' }); | ||
``` | ||
Calling `identify` on the client stream kicks off a handshake process with the room, and thus we should wait for the client to become ready before writing any messages: | ||
```js | ||
client.once('ready', function(metadata) { | ||
client.write('hello'); | ||
}); | ||
``` | ||
For the full example, see [examples/hello.js](examples/hello.js). |
@@ -15,3 +15,3 @@ var assert = require('assert'), | ||
it('should be able to connect to the room', function() { | ||
client = chat.client(room.connect()); | ||
client = chat.client(room); | ||
}); | ||
@@ -72,2 +72,14 @@ | ||
}); | ||
it('should report metadata in the ready event', function(done) { | ||
var nick = randomName().replace(/\s/g, ''), | ||
client4 = chat.client(room, { nick: nick }); | ||
client4.once('ready', function(metadata) { | ||
assert(metadata, 'No metadata received in the ready event'); | ||
assert.equal(metadata.connections.length, 4, 'Did not get the expected 4 connections'); | ||
done(); | ||
}); | ||
}); | ||
}); |
34898
19
827
52