Skip to content

Commit

Permalink
Emit friends event when friends_str message is received.
Browse files Browse the repository at this point in the history
  • Loading branch information
ttezel committed Jan 30, 2016
1 parent 32aa62e commit 1687130
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,11 @@ stream.on('user_withheld', function (withheldMsg) {

##event: 'friends'

Emitted when Twitter sends the ["friends" preamble](https://dev.twitter.com/streaming/overview/messages-types#user_stream_messsages) when connecting to a user stream. This message contains a list of the user's friends, represented as an array of user ids.
Emitted when Twitter sends the ["friends" preamble](https://dev.twitter.com/streaming/overview/messages-types#user_stream_messsages) when connecting to a user stream. This message contains a list of the user's friends, represented as an array of user ids. If the [stringify_friend_ids](https://dev.twitter.com/streaming/overview/request-parameters#stringify_friend_id) parameter is set, the friends
list preamble will be returned as Strings (instead of Numbers).

```javascript
var stream = T.stream('user', { stringify_friend_ids: true })
stream.on('friends', function (friendsMsg) {
//...
})
Expand Down Expand Up @@ -506,6 +508,10 @@ THE SOFTWARE.

## Changelog

###2.1.6
* Emit `friends` event for `friends_str` message received when a user stream is requested with `stringify_friend_ids=true`.


###2.1.5
* Support config-based request timeout.

Expand Down
2 changes: 1 addition & 1 deletion lib/streaming-api-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ StreamingAPIConnection.prototype._setupParser = function () {
else if (msg.warning) { self.emit('warning', msg) }
else if (msg.status_withheld) { self.emit('status_withheld', msg) }
else if (msg.user_withheld) { self.emit('user_withheld', msg) }
else if (msg.friends) { self.emit('friends', msg) }
else if (msg.friends || msg.friends_str) { self.emit('friends', msg) }
else if (msg.direct_message) { self.emit('direct_message', msg) }
else if (msg.event) {
self.emit('user_event', msg)
Expand Down
17 changes: 17 additions & 0 deletions tests/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@ describe('streaming API direct message events', function () {
})
})

describe('streaming API friends preamble', function () {
it('returns an array of strings if stringify_friend_ids is true', function (done) {
var twit = new Twit(config1);
var stream = twit.stream('user', { stringify_friend_ids: true });
stream.on('friends', function (friendsObj) {
assert(friendsObj)
assert(friendsObj.friends_str)
if (friendsObj.friends_str.length) {
assert.equal(typeof friendsObj.friends_str[0], 'string')
} else {
console.log('\nEmpty friends preamble:', friendsObj, '. Make some friends on Twitter! ^_^')
}
done()
})
})
})

describe('streaming API bad request', function (done) {
it('emits an error for a 401 response', function (done) {
var badCredentials = {
Expand Down

0 comments on commit 1687130

Please sign in to comment.