Java client for GetStream.io
Java Shell
Permalink
Failed to load latest commit information.
.travis
stream-core
stream-repo-apache
stream-repo-okhttp
.gitignore
.travis.yml
README.md
pom.xml

README.md

stream-java

Build Status

stream-java is a Java client for Stream.

The Stream's Java client come in two different flavours, you should decide which one to drag into your project. Those two implementations differ according to the underlying library used to handle HTTP connections:

  • stream-repo-apache uses Apache HttpClient and we recommend it for backend applications. Apache HttpClient is a mature, reliable and rock-solid HTTP library.
  • stream-repo-okhttp uses Square's OkHttp which is lightweight, powerful and mobile-oriented HTTP library. We recommend it for mobile application.

Installation

If you decide to go for the Apache HttpClient implementation, add the following dependency to your pom.xml:

<dependency>
    <groupId>io.getstream.client</groupId>
    <artifactId>stream-repo-apache</artifactId>
    <version>1.2.1</version>
</dependency>

or in your build.gradle:

compile 'io.getstream.client:stream-repo-apache:1.2.1'

Instead, if you opted for the OkHttp implementation please add it to your pom.xml

<dependency>
    <groupId>io.getstream.client</groupId>
    <artifactId>stream-repo-okhttp</artifactId>
    <version>1.2.1</version>
</dependency>

or in your build.gradle:

compile 'io.getstream.client:stream-repo-okhttp:1.2.1'

In case you want to download the artifact and put it manually into your project, you can download it from here.

Snapshots of the development version are available in Sonatype snapshots repository.

Usage

/**
 * Instantiate a new client to connect to us east API endpoint
 * Find your API keys here https://getstream.io/dashboard/
 **/

StreamClient streamClient = new StreamClientImpl(new ClientConfiguration(), "<API_KEY>", "<API_SECRET>");

Create a new Feed

/* Instantiate a feed object */
Feed feed = streamClient.newFeed("user", "1");

Working with Activities

/* Create an activity service */
FlatActivityServiceImpl<SimpleActivity> flatActivityService = feed.newFlatActivityService(SimpleActivity.class);

/* Get activities from 5 to 10 (using offset pagination) */
FeedFilter filter = new FeedFilter.Builder().withLimit(5).withOffset(5).build();
List<SimpleActivity> activities = flatActivityService.getActivities(filter).getResults();

/* Filter on an id less than the given UUID */
aid = "e561de8f-00f1-11e4-b400-0cc47a024be0";
FeedFilter filter = new FeedFilter.Builder().withIdLowerThan(aid).withLimit(5).build();
List<SimpleActivity> activities = flatActivityService.getActivities(filter).getResults();

/* Create a new activity */
SimpleActivity activity = new SimpleActivity();
activity.setActor("user:1");
activity.setObject("tweet:1");
activity.setVerb("tweet");
activity.setForeignId("tweet:1");
SimpleActivity response = flatActivityService.addActivity(activity);

/* Remove an activity by its id */
feed.deleteActivity("e561de8f-00f1-11e4-b400-0cc47a024be0");

/* Remove activities by their foreign_id */
feed.deleteActivityByForeignId("tweet:1");

In case you want to add a single activity to multiple feeds, you can use the batch feature addToMany:

/* Batch adding activities to many feeds */
flatActivityService.addActivityToMany(ImmutableList.<String>of("user:1", "user:2").asList(), myActivity);

The API client allows you to send activities with custom field as well, you can find a complete example here

Follow and Unfollow

/* Follow another feed */
feed.follow(flat", "42");

/* Stop following another feed */
feed.unfollow(flat", "42");

/* Retrieve first 10 followers of a feed */
FeedFilter filter = new FeedFilter.Builder().withLimit(10).build();
List<FeedFollow> followingPaged = feed.getFollowing(filter);

/* Retrieve the first 10 followed feeds */
FeedFilter filter = new FeedFilter.Builder().withLimit(10).build();
List<FeedFollow> followingPaged = feed.getFollowing(filter);

In case you want to send to Stream a long list of following relationships you can use the batch feature followMany:

/* Batch following many feeds */
FollowMany followMany = new FollowMany.Builder()
    .add("user:1", "user:2")
    .add("user:1", "user:3")
    .add("user:1", "user:4")
    .add("user:2", "user:3")
    .build();
feed.followMany(followMany);

Client token

In order to generate a token for client side usage (e.g. JS client), you can use the following code:

/* Generating tokens for client side usage */
String token = feed.getToken();

Further references

For more examples have a look here.

Docs are available on GetStream.io.

Javadocs are available here.