Stream-JS
![codecov](https://codecov.io/gh/GetStream/stream-js/branch/master/graph/badge.svg)
![NPM](https://nodei.co/npm/getstream.png)
stream-js is the official JavaScript client for Stream, a web service for building scalable newsfeeds and activity streams.
Note that there is also a higher level Node integration which hooks into your ORM.
You can sign up for a Stream account at https://getstream.io/get_started.
Installation
Install from NPM/YARN
npm install getstream
or if you are using yarn
yarn install getstream
Using JS deliver
<script src="https://cdn.jsdelivr.net/npm/getstream/dist/js_min/getstream.js"></script>
:warning: This will pull the latest which can be breaking for your application. Always pin a specific version as follows:
<script src="https://cdn.jsdelivr.net/npm/getstream@5.0.0/dist/js_min/getstream.js"></script>
Install by downloading the JS file
JS /
Minified JS
:warning: Beware about the version you're pulling. It's the latest by default which can break your app anytime.
Full documentation
Documentation for this JavaScript client are available at the Stream website.
Using with React Native
This package can be integrated into React Native applications. Remember to not expose the App Secret in browsers, "native" mobile apps, or other non-trusted environments.
Usage
API client setup Node
import { connect } from 'getstream';
const { connect } = require('getstream');
const client = connect('YOUR_API_KEY', 'API_KEY_SECRET');
const client = connect('YOUR_API_KEY', 'API_KEY_SECRET', 'APP_ID', { location: 'us-east', timeout: 15000 });
API client setup Node + Browser
If you want to use the API client directly on your web/mobile app you need to generate a user token server-side and pass it.
Server-side token generation
import { connect } from 'getstream';
const { connect } = require('getstream');
const client = stream.connect('YOUR_API_KEY', 'API_KEY_SECRET');
const client = stream.connect('YOUR_API_KEY', 'API_KEY_SECRET', 'APP_ID', { location: 'us-east', timeout: 15000 });
const userToken = client.createUserToken('the-user-id');
Client API init
import { connect } from 'getstream';
const { connect } = require('getstream');
const client = stream.connect('apikey', userToken, 'appid');
Examples
user1 = client.feed('user', '1');
user1.get({ limit: 5, offset: 5 });
user1.get({ limit: 5, id_lt: 'e561de8f-00f1-11e4-b400-0cc47a024be0' });
user1
.get({ limit: 5, id_lt: 'e561de8f-00f1-11e4-b400-0cc47a024be0' })
.then(function (body) {
})
.catch(function (reason) {
});
activity = { actor: 1, verb: 'tweet', object: 1, foreign_id: 'tweet:1' };
user1.addActivity(activity);
activity = {
actor: 1,
verb: 'run',
object: 1,
foreign_id: 'run:1',
course: { name: 'Golden Gate park', distance: 10 },
participants: ['Thierry', 'Tommaso'],
started_at: new Date(),
};
user1.addActivity(activity);
user1.removeActivity('e561de8f-00f1-11e4-b400-0cc47a024be0');
user1.removeActivity({ foreignId: 'tweet:1' });
notification1 = client.feed('notification', '1');
params = { mark_read: true };
notification1.get(params);
params = { mark_seen: true };
notification1.get(params);
user1.follow('flat', '42');
user1.unfollow('flat', '42');
user1.unfollow('flat', '42', { keepHistory: true });
user1.follow('flat', '42', { limit: 0 });
user1.followers({ limit: '10', offset: '10' });
user1.following({ limit: '10', offset: '0' });
user1.follow('flat', '42');
activities = [
{ actor: 1, verb: 'tweet', object: 1 },
{ actor: 2, verb: 'tweet', object: 3 },
];
user1.addActivities(activities);
to = ['user:2', 'user:3'];
activity = {
to: to,
actor: 1,
verb: 'tweet',
object: 1,
foreign_id: 'tweet:1',
};
user1.addActivity(activity);
feeds = ['flat:1', 'flat:2', 'flat:3', 'flat:4'];
activity = {
actor: 'User:2',
verb: 'pin',
object: 'Place:42',
target: 'Board:1',
};
client.addToMany(activity, feeds);
follows = [
{ source: 'flat:1', target: 'user:1' },
{ source: 'flat:1', target: 'user:2' },
{ source: 'flat:1', target: 'user:3' },
];
client.followMany(follows);
set = {
'product.price': 19.99,
shares: {
facebook: '...',
twitter: '...',
},
};
unset = ['daily_likes', 'popularity'];
client.activityPartialUpdate({
id: '54a60c1e-4ee3-494b-a1e3-50c06acb5ed4',
set: set,
unset: unset,
});
client.activityPartialUpdate({
foreignID: 'product:123',
time: '2016-11-10T13:20:00.000000',
set: set,
unset: unset,
});
impression = {
content_list: ['tweet:1', 'tweet:2', 'tweet:3'],
user_data: 'tommaso',
location: 'email',
feed_id: 'user:global',
};
engagement = {
content: 'tweet:2',
label: 'click',
position: 1,
user_data: 'tommaso',
location: 'email',
feed_id: 'user:global',
};
events = [impression, engagement];
redirectUrl = client.createRedirectUrl('http://google.com', 'user_id', events);
client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, ['feed:1234']);
client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, ['feed:1234']);
client.feed('user', 'ken').updateActivityToTargets('foreign_id:1234', timestamp, null, null, ['feed:1234']);
Realtime (Faye)
Stream uses Faye for realtime notifications. Below is quick guide to subscribing to feed changes
const { connect } = require('getstream');
const client = connect('YOUR_API_KEY', userToken, 'APP_ID');
const user1 = client.feed('user', '1');
const subscription = user1.subscribe(function (data) {
console.log(data);
});
subscription.cancel();
Docs are available on GetStream.io.
Node version requirements & Browser support
This API Client project requires Node.js v10 at a minimum.
The project is supported in line with the Node.js Foundation Release Working Group.
See the github action configuration for details of how it is built, tested and packaged.
Contributing
See extensive at test documentation for your changes.
You can find generic API documentation enriched by code snippets from this package at http://getstream.io/docs/?language=js
Copyright and License Information
Copyright (c) 2015-2020 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.