Stream Node.js
stream-node-orm is a Node.js (Mongoose & Waterline) client for Stream.
You can sign up for a Stream account at https://getstream.io/get_started.
Note there is also a lower level Node.js - Stream integration library which is suitable for all JavaScript applications.
Build Activity Streams & News Feeds
You can build:
- Activity streams such as those seen on Github
- A Twitter style newsfeed
- A feed like Instagram or Pinterest
- Facebook style newsfeeds
- A notification system
Supported ORMs
Stream node currently supports:
- Mongoose (full support, both serialization and enrichment)
- Waterline (partial support, enrichment only)
Demo
You can check out our example app on Github https://github.com/GetStream/Stream-Example-Nodejs
Installation
Step 1 - NPM
Install getstream_node package with npm:
npm install getstream-node --save
Step 2 - Config file
Copy getstream.js
config file from node_modules/getstream-node
into the root directory of your application
Make sure you require the getstream-node early on in your application (eg. in app.js)
Step 3 - Get your API key
Login with Github on getstream.io and edit the configuration values for
apiKey
, apiSecret
and apiAppId
in your getstream.js
file (you can find them in the dashboard).
Model integration
Stream Node.js can automatically publish new activities to your feeds. To do that you only need to register the models you want to publish with this library.
var stream = require('getstream-node');
var tweetSchema = Schema({
text : String,
user : { type: Schema.Types.ObjectId, ref: 'User' }
});
tweetSchema.plugin(stream.mongoose.activity);
// register your mongoose connection with the library
stream.mongoose.setupMongoose(mongoose);
Every time a Tweet is created it will be added to the user's feed. Users which follow the given user will also automatically get the new tweet in their feeds.
Activity Fields
Models are stored in feeds as activities. An activity is composed of at least the following fields: actor, verb, object, time. You can also add more custom data if needed. The Activity mixin will try to set things up automatically:
object is a reference to the model instance actor is a reference to the user attribute of the instance verb is a string representation of the class name
By default the actor field will look for an attribute called user or actor and a field called created_at to track creation time. If your user field is called differently you'll need to tell us where to look for it. Below shows an example how to set things up if your user field is called author.
var tweetSchema = Schema({
text : String,
author : { type: Schema.Types.ObjectId, ref: 'User' }
});
tweetSchema.plugin(stream.mongoose.activity);
tweetSchema.methods.activityActorProp = function() {
return 'author';
}
Customizing the Activity
Sometimes you'll want full control over the activity that's send to getstream.io. To do that you can overwrite the default createActivity method on the model
tweetSchema.methods.createActivity = function() {
// this is the default createActivity code, customize as you see fit.
var activity = {};
var extra_data = this.activityExtraData();
for (var key in extra_data) {
activity[key] = extra_data[key];
}
activity.to = (this.activityNotify() || []).map(function(x){return x.id});
activity.actor = this.activityActor();
activity.verb = this.activityVerb();
activity.object = this.activityObject();
activity.foreign_id = this.activityForeignId();
if (this.activityTime()) {
activity.time = this.activityTime();
}
return activity;
}
Feed Manager
This packages comes with a feed_manager class that helps with all common feed operations.
Feeds Bundled with feed_manager
To get you started the manager has 4 feeds pre-configured. You can add more feeds if your application needs it. The three feeds are divided in three categories.
User Feed:
The user feed stores all activities for a user. Think of it as your personal Facebook page. You can easily get this feed from the manager.
FeedManager.getUserFeed(req.user.id);
News Feeds:
The news feeds store the activities from the people you follow. There is both a flat newsfeed (similar to twitter) and an aggregated newsfeed (like facebook).
var flatFeed = FeedManager.getNewsFeeds(foundUser._id)['timeline_flat'];
var aggregatedFeed = FeedManager.getNewsFeeds(req.user.id)['timeline_aggregated'];
Notification Feed:
The notification feed can be used to build notification functionality.
Below we show an example of how you can read the notification feed.
var notificationFeed = FeedManager.getNotificationFeed(req.user.id);
By default the notification feed will be empty. You can specify which users to notify when your model gets created. In the case of a retweet you probably want to notify the user of the parent tweet.
tweetSchema.methods.activityNotify = function() {
if (this.isRetweet) {
target_feed = FeedManager.getNotificationFeed(this.parent.author.id);
return [target_feed];
}
};
Another example would be following a user. You would commonly want to notify the user which is being followed.
followSchema.methods.activityNotify = function() {
target_feed = FeedManager.getNotificationFeed(this.target._id);
return [target_feed];
};
Follow a Feed
To follow the created newsfeeds you need to notify the system about follow relationships. The manager comes with APIs to let a user's news feeds follow another user's feed. This code lets the current user's flat and aggregated feeds follow the target_user's personal feed.
FeedManager.followUser(userId, targetId);
Showing the Newsfeed
Activity Enrichment
When you read data from feeds, a like activity will look like this:
{'actor': 'User:1', 'verb': 'like', 'object': 'Like:42'}
This is far from ready for usage in your template. We call the process of loading the references from the database enrichment. An example is shown below:
router.get('/flat', ensureAuthenticated, function(req, res, next){
var flatFeed = FeedManager.getNewsFeeds(req.user.id)['timeline_flat'];
flatFeed.get({})
.then(function (body) {
var activities = body.results;
return StreamBackend.enrichActivities(activities);
})
.then(function (enrichedActivities) {
return res.render('feed', {location: 'feed', user: req.user, activities: enrichedActivities, path: req.url});
})
.catch(next)
});
});
Promises are used to pipe the asynchronous result of flatFeed.get
and StreamBackend.enrichActivities
through our code.
Temporarily Disabling the Model Sync
Model synchronization can be disabled manually via environment variable.
NODE_ENV=test npm test
Automatically Populate Paths:
You can automatically populate paths during enrichment via the pathsToPopulate static.
tweetSchema.statics.pathsToPopulate = function() {
return ['link'];
};
Full Documentation and Low Level APIs Access
When needed you can also use the low level JavaScript API directly. Documentation is available at the Stream website.
var streamNode = require('getstream-node');
var client = streamNode.FeedManager.client
// client.addActivity, client.removeActivity etc are all available
Enrichment
You can use the enrichment capabilities of this library directly.
var streamNode = require('getstream-node');
var streamMongoose = new streamNode.MongooseBackend()
// or
var streamWaterline = new streamNode.WaterlineBackend()
// now enrich the activities
streamWaterline.enrichActivities(activities).then(function(enrichedActivities) {
res.json({'results': enrichedActivities})
}).catch(function(err) {
sails.log.error('enrichment failed', err)
return res.serverError('failed to load articles in the feed')
})
Customizing Enrichment (since 1.4.0)
By default the enrichment system assumes that you're referencing items by their id. Sometimes you'll want to customize this behavior. You might for instance use a username instead of an id. Alternatively you might mant to use a caching layer instead of the ORM for loading the data. The example below shows how to customize the lookup for all User entries.
// subclass streamMongoose
function streamCustomEnrichment() {};
streamCustomEnrichment.prototype = {
loadUserFromStorage: function(modelClass, objectsIds, callback) {
var found = {};
var paths = [];
if (typeof(modelClass.pathsToPopulate) === 'function') {
var paths = modelClass.pathsToPopulate();
}
// Here's the magic, use a username instead of id
modelClass.find({
username: {
$in: objectsIds
}
}).populate(paths).exec(function(err, docs) {
for (var i in docs) {
found[docs[i]._id] = docs[i];
}
callback(err, found);
});
}
}
util.inherits(streamCustomEnrichment, streamNode.mongoose.Backend);
Contributing
Running tests:
npm test
Releasing
Make sure your working directory is clean and run:
npm install
npm version [ major | minor | patch ]
npm publish
Supported Node.js Versions
v10.x
v12.x
v14.x
Copyright and License Information
Copyright (c) 2015-2017 Stream.io Inc, and individual contributors. All rights reserved.
See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES.