Getting Started

Learn How Easy It Can Be To Build Scalable Newsfeeds and Activity Streams

Stream has official clients for JS/Node, Ruby, Python, PHP, Go, and Java. There are framework integrations available for Rails, Django, Laravel, Doctrine, Zend, and Node. In addition to the official clients, the community has built clients for.NET and Scala.

Setup

Let’s get set up! First, install the client as specified below:

// CocoaPods
pod 'GetStream', '~> 2.0'

// Carthage
github "GetStream/stream-swift"

All source code can be found on GitHub.

To instantiate the client you need an API key and secret. You can find the key and secret on the dashboard. The examples below already include your key and secret.

// import GetStream

// Setup Stream client (server side)
let client = Client(apiKey: "{{ api_secret }}", appId: "{{ app_id }}", token: "{{ feed_token }}")

If you want to use Stream on your mobile or web application, you need to generate a token server-side that the JS/Swift/Java client can use to authenticate as a user of your application.

Generate User Token Server-Side

This code generates the token for one of your users; a common place to do this is at signup or login. The token is then passed to the frontend.

var token = client.CreateUserToken("the-user-id");

Use Stream API client-side (JS, Swift)

// import GetStream

// Setup Stream client.
let client = Client(
 apiKey: "{{ api_key }}", 
 appId: "{{ app_id }}", 
 token: "{{ feed_token }}"
)

More details about authentication can be found in the REST docs

Quick Start

The quick start below shows you how to build a scalable social network. It highlights the most common API calls:

// Setup a shared Stream Client.
let client = Client(apiKey: "{{ api_secret }}", appId: "{{ app_id }}", token: "{{ feed_token }}")

// Setup a Stream user, when your user was logged in.
Client.shared.setupUser(token: token) { _ in 
  // Do all your requests from here. Reload feeds and etc.
}

// Create a user feed.
// ⚠️ Make sure your feeds are instance variables to wait for the result: `var userFeed: FlatFeed?`
userFeed = Client.shared.flatFeed(feedSlug: "user")

// Create an Activity. You can make own Activity class or struct with custom properties.
let activity = Activity(actor: User.current!, verb: "add", object: "picture:10", foreignId: "picture:10")

userFeed?.add(activity) { result in
  // A result of the adding of the activity.
  print(result)
}

// Create a following relationship between "timeline" feed and "user" feed:
timelineFeed = Client.shared.flatFeed(feedSlug: "timeline")

timelineFeed?.follow(toTarget: userFeed!.feedId, activityCopyLimit: 1) { result in
  print(result)
}

// Read timeline and user's post appears in the feed:
timelineFeed?.get(pagination: .limit(10)) { result in
  let response = try! result.get()
  print(response.results)
}

// Remove an activity by referencing it's foreignId
userFeed?.remove(foreignId: "picture:10") { result in
  print(result)
}

That was a good deal of information at once. The getting started docs provide a more detailed and interactive explanation.

© Getstream.io, Inc. All Rights Reserved.