• Stars
    star
    257
  • Rank 158,728 (Top 4 %)
  • Language
    Ruby
  • License
    BSD 3-Clause "New...
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rails Client - Build Activity Feeds & Streams with GetStream.io

Stream Rails

build Gem Version

stream-rails is a Ruby on Rails client for Stream.

You can sign up for a Stream account at https://getstream.io/get_started.

Note there is also a lower level Ruby - Stream integration library which is suitable for all Ruby applications.

💡 This is a library for the Feeds product. The Chat SDKs can be found here.

Activity Streams & Newsfeeds

What you can build:

  • Activity streams such as seen on Github
  • A twitter style newsfeed
  • A feed like instagram/ pinterest
  • Facebook style newsfeeds
  • A notification system

Demo

You can check out our example app built using this library on Github https://github.com/GetStream/Stream-Example-Rails

Table of Contents

Gem installation

You can install stream_rails as you would any other gem:

gem install stream_rails

or in your Gemfile:

gem 'stream_rails'

This library is tested against and fully supports the following Rails versions:

  • 5.0
  • 5.2
  • 6.0
  • 6.1

Setup

Login with Github on getstream.io and get your api_key and api_secret from your app configuration (Dashboard screen).

Then you can add the StreamRails configuration in config/initializers/stream_rails.rb

require 'stream_rails'

StreamRails.configure do |config|
  config.api_key      = "YOUR API KEY"
  config.api_secret   = "YOUR API SECRET"
  config.timeout      = 30                  # Optional, defaults to 3
  config.location     = 'us-east'           # Optional, defaults to 'us-east'
  config.api_hostname = 'stream-io-api.com' # Optional, defaults to 'stream-io-api.com'
  # If you use custom feed names, e.g.: timeline_flat, timeline_aggregated,
  # use this, otherwise omit:
  config.news_feeds = { flat: "timeline_flat", aggregated: "timeline_aggregated" }
  # Point to the notifications feed group providing the name, omit if you don't
  # have a notifications feed
  config.notification_feed = "notification"
end

Supported ORMs

ActiveRecord

The integration will look as follows:

class Pin < ActiveRecord::Base
  include StreamRails::Activity
  as_activity

  def activity_object
    self.item
  end
end

Sequel

Please, use Sequel ~5.

The integration will look as follows:

class Pin < Sequel::Model
  include StreamRails::Activity
  as_activity

  def activity_object
    self.item
  end
end

Model configuration

Include StreamRails::Activity and add as_activity to the model you want to integrate with your feeds.

class Pin < ActiveRecord::Base
  belongs_to :user
  belongs_to :item

  validates :item, presence: true
  validates :user, presence: true

  include StreamRails::Activity
  as_activity

  def activity_object
    self.item
  end

end

Everytime a Pin is created it will be stored in the feed of the user that created it. When a Pin instance is deleted, the feed will be removed as well.

Activity fields

ActiveRecord models are stored in your feeds as activities; Activities are objects that tell the story of a person performing an action on or with an object, in its simplest form, an activity consists of an actor, a verb, and an object. In order for this to happen your models need to implement these methods:

#activity_object the object of the activity (eg. an AR model instance)

#activity_actor the actor performing the activity -- this value also provides the feed name and feed ID to which the activity will be added.

For example, let's say a Pin was a polymorphic class that could belong to either a user (e.g. User ID: 1) or a company (e.g. Company ID: 1). In that instance, the below code would post the pin either to the user:1 feed or the company:1 feed based on its owner.

class Pin < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true
  belongs_to :item

  include StreamRails::Activity
  as_activity

  def activity_actor
    self.owner
  end

  def activity_object
    self.item
  end

end

The activity_actor defaults to self.user

#activity_verb the string representation of the verb (defaults to model class name)

Here's a more complete example of the Pin class:

class Pin < ActiveRecord::Base
  belongs_to :author
  belongs_to :item

  include StreamRails::Activity
  as_activity

  def activity_actor
    self.author
  end

  def activity_object
    self.item
  end

end

Activity extra data

Often you'll want to store more data than just the basic fields. You achieve this by implementing #activity_extra_data in your model.

class Pin < ActiveRecord::Base
  belongs_to :author
  belongs_to :item

  include StreamRails::Activity
  as_activity

  def activity_extra_data
    {'is_retweet' => self.is_retweet}
  end

  def activity_object
    self.item
  end

end

Activity creation

If you want to control when to create an activity you should implement the #activity_should_sync? method in your model.

class Pin < ActiveRecord::Base
  belongs_to :author
  belongs_to :item

  include StreamRails::Activity
  as_activity

  def activity_should_sync?
    self.published
  end

  def activity_object
    self.item
  end

end

This will create an activity only when self.published is true.

Feed manager

stream_rails comes with a Feed Manager class that helps with all common feed operations. You can get an instance of the manager with StreamRails.feed_manager.

feed = StreamRails.feed_manager.get_user_feed(current_user.id)

Feeds bundled with feed_manager

To get you started the manager has 4 feeds pre-configured. You can add more feeds if your application requires it. Feeds are divided into 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.

feed = StreamRails.feed_manager.get_user_feed(current_user.id)
News feeds:

News feeds store activities from the people you follow. There is both a flat newsfeed (similar to twitter) and an aggregated newsfeed (like facebook).

feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:flat]
aggregated_feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:aggregated]
Notification feed:

The notification feed can be used to build notification functionality.

Notification feed

Below we show an example of how you can read the notification feed.

notification_feed = StreamRails.feed_manager.get_notification_feed(current_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.

class Pin < ActiveRecord::Base
  belongs_to :author
  belongs_to :item

  include StreamRails::Activity
  as_activity

  def activity_notify
    if self.is_retweet
      [StreamRails.feed_manager.get_notification_feed(self.parent.user_id)]
    end
  end

  def activity_object
    self.item
  end

end

Another example would be following a user. You would commonly want to notify the user which is being followed.

class Follow < ActiveRecord::Base
  belongs_to :user
  belongs_to :target

  validates :target_id, presence: true
  validates :user, presence: true

  include StreamRails::Activity
  as_activity

  def activity_notify
    [StreamRails.feed_manager.get_notification_feed(self.target_id)]
  end

  def activity_object
    self.target
  end

end

Follow a feed

In order to populate newsfeeds, you need to notify the system about follow relationships.

The current user's flat and aggregated feeds will follow the target_user's user feed, with the following code:

StreamRails.feed_manager.follow_user(user_id, target_id)

Showing the newsfeed

Activity enrichment

When you read data from feeds, a pin activity will look like this:

{ "actor": "User:1", "verb": "like", "object": "Item: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:

enricher = StreamRails::Enrich.new

feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:flat]
results = feed.get()['results']
activities = enricher.enrich_activities(results)

A similar method called enrich_aggregated_activities is available for aggregated feeds.

enricher = StreamRails::Enrich.new

feed = StreamRails.feed_manager.get_news_feeds(current_user.id)[:aggregated]
results = feed.get()['results']
activities = enricher.enrich_aggregated_activities(results)

If you have additional metadata in your activity (by overriding activity_extra_data in the class where you add the Stream Activity mixin), you can also enrich that field's data by doing the following:

Step One: override the activity_extra_data method from our mixin:

class Pin < ActiveRecord::Base
  include StreamRails::Activity
  as_activity

  attr_accessor :extra_data

  def activity_object
    self.item
  end

  # override this method to add metadata to your activity
  def activity_extra_data
    @extra_data
  end
end

Now we'll create a 'pin' object which has a location metadata field. In this example, we will also have a location table and model, and we set up our metadata in the extra_data field. It is important that the symbol of the metadata as well as the value of the meta data match this pattern. The left half of the string:string metadata value when split on : must also match the name of the model.

We must also tell the enricher to also fetch locations when looking through our activities

boulder = Location.new
boulder.name = "Boulder, CO"
boulder.save!

# tell the enricher to also do a lookup on the `location` model
enricher.add_fields([:location])

pin = Pin.new
pin.user = @tom
pin.extra_data = {:location => "location:#{boulder.id}"}

When we retrieve the activity later, the enrichment process will include our location model as well, giving us access to attributes and methods of the location model:

place = activity[:location].name
# Boulder, CO

Templating

Now that you've enriched the activities you can render them in a view. For convenience we include a basic view:

<div class="container">
    <div class="container-pins">
        <% for activity in @activities %>
            <%= render_activity activity %>
        <% end %>
    </div>
</div>

The render_activity view helper will render the activity by picking the partial activity/_pin for a pin activity, aggregated_activity/_follow for an aggregated activity with verb follow.

The helper will automatically send activity to the local scope of the partial; additional parameters can be sent as well as use different layouts, and prefix the name

e.g. renders the activity partial using the small_activity layout:

<%= render_activity activity, :layout => "small_activity" %>

e.g. prefixes the name of the template with "notification_":

<%= render_activity activity, :prefix => "notification_" %>

e.g. adds the extra_var to the partial scope:

<%= render_activity activity, :locals => {:extra_var => 42} %>

e.g. renders the activity partial using the notifications partial root, which will render the partial with the path notifications/#{ACTIVITY_VERB}

<%= render_activity activity, :partial_root => "notifications" %>

Pagination

For simple pagination you can use the stream-ruby API, as follows in your controller:

  StreamRails.feed_manager.get_news_feeds(current_user.id)[:flat] # Returns a Stream::Feed object
  results = feed.get(limit: 5, offset: 5)['results']

Disable model tracking

You can disable model tracking (eg. when you run tests) via StreamRails.configure

require 'stream_rails'

StreamRails.enabled = false

Running specs

From the project root directory:

./bin/run_tests.sh

Full documentation and Low level APIs access

When needed you can also use the low level Ruby API directly. Documentation is available at the Stream website.

Copyright and License Information

Copyright (c) 2014-2021 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.

More Repositories

1

Winds

A Beautiful Open Source RSS & Podcast App Powered by Getstream.io
JavaScript
8,813
star
2

stream-chat-android

💬 Android Chat SDK ➜ Stream Chat API. UI component libraries for chat apps. Kotlin & Jetpack Compose messaging SDK for Android chat
Kotlin
1,443
star
3

vg

Virtualgo: Easy and powerful workspace based development for go
Go
1,311
star
4

whatsApp-clone-compose

📱 WhatsApp clone project demonstrates modern Android development built with Jetpack Compose and Stream Chat/Video SDK for Compose.
Kotlin
1,254
star
5

stream-react-example

Use React and Redux to build your own feature-rich and scalable social network app! Visit cabin.getstream.io for an overview of all 8 tutorials and a live demo.
HTML
923
star
6

stream-chat-flutter

Flutter Chat SDK - Build your own chat app experience using Dart, Flutter and the Stream Chat Messaging API.
Dart
839
star
7

stream-chat-react-native

💬 React-Native Chat SDK ➜ Stream Chat. Includes a tutorial on building your own chat app experience using React-Native, React-Navigation and Stream
TypeScript
792
star
8

purposeful-ios-animations

Meaningful iOS animations built to inspire you in creating useful animations for your apps. Each of the animations here was cloned with SwiftUI. Have you seen an app animation you love to rebuild and add to this repo?, contact [@amos_gyamfi](https://twitter.com/amos_gyamfi) and [@stefanjblos](https://twitter.com/stefanjblos) on Twitter.
Swift
763
star
9

stream-chat-swift

💬 iOS Chat SDK in Swift - Build your own app chat experience for iOS using the official Stream Chat API
Swift
750
star
10

swiftui-spring-animations

This repository serves as your reference and complete guide for SwiftUI Spring Animations. It demonstrates use cases for the various types of spring animations and spring parameters. No more guessing the values of the parameters for spring animations you create for your next iOS app.
Swift
692
star
11

webrtc-android

🛰️ A versatile WebRTC pre-compiled Android library that reflects the recent WebRTC updates to facilitate real-time video chat for Android and Compose.
Kotlin
578
star
12

stream-chat-react

React Chat SDK ➜ Stream Chat 💬
TypeScript
552
star
13

awesome-saas-services

A curated list of the best in class SaaS services for developers and business owners.
475
star
14

stream-django

Django Client - Build Activity Feeds & Streams with GetStream.io
Python
453
star
15

sketchbook-compose

🎨 Jetpack Compose canvas library that helps you draw paths, images on canvas with color pickers and palettes.
Kotlin
442
star
16

avatarview-android

✨ Supports loading profile images with fractional styles, shapes, borders, indicators, and initials for Android.
Kotlin
432
star
17

webrtc-in-jetpack-compose

📱 This project demonstrates WebRTC protocol to facilitate real-time video communications with Jetpack Compose.
Kotlin
402
star
18

stream-video-android

📲 Android Video SDK. Stream's versatile Core + Compose UI component libraries that allow you to build video calling, audio room, and, live streaming apps based on Webrtc running on Stream's global edge network.
Kotlin
374
star
19

AvengersChat

💙 Android sample Avengers chat application using Stream Chat SDK based on MVVM (ViewModel, Coroutines, Room, Hilt, Repository) architecture.
Kotlin
367
star
20

stream-draw-android

🛥 Stream Draw is a real-time multiplayer drawing & chat game app built entirely with Jetpack Compose.
Kotlin
341
star
21

stream-chat-swiftui

SwiftUI Chat SDK ➜ Stream Chat 💬
Swift
329
star
22

stream-js

JS / Browser Client - Build Activity Feeds & Streams with GetStream.io
JavaScript
329
star
23

effects-library

The Effects Library allows developers to create sophisticated and realistic particle systems such as snow, fire, rain, confetti, fireworks, and smoke with no or minimal effort.
Swift
324
star
24

react-native-example

React Native Activity Feed example application
JavaScript
321
star
25

stream-laravel

Laravel Client - Build Activity Feeds & Streams with GetStream.io
PHP
314
star
26

flutter-samples

A collection of sample apps that use Stream
Dart
298
star
27

Streamoji

:godmode: Custom emoji rendering library for iOS apps with support for GIF & still images - plug-in extension for UITextView - performance, cache ✅ - Made with 💘 by @GetStream
Swift
248
star
28

react-native-bidirectional-infinite-scroll

📜 React Native - Bidirectional Infinite Smooth Scroll
TypeScript
220
star
29

WhatsApp-Clone-Android

Tutorial which teaches you how to build a whatsapp chat clone on android using Kotlin, viewmodels, navigation component and Stream
Kotlin
218
star
30

butterfly

🦋 Butterfly helps you to build adaptive and responsive UIs for Android with Jetpack WindowManager.
Kotlin
214
star
31

slack-clone-react-native

Build a slack clone using react-native, Stream and react-navigation
JavaScript
206
star
32

react-native-activity-feed

Official React Native SDK for Activity Feeds
JavaScript
194
star
33

motionscape-app

MotionScape is your animations playground as a developer. You can see all animations and their parameters in effect with beautifully designed and handcrafted animation examples.
Swift
167
star
34

Android-Samples

📕 Provides a list of samples that utilize modern Android tech stacks and Stream Chat SDK for Android and Compose.
Kotlin
159
star
35

node-express-mongo-api

Starter project for a REST API with Node.js, Express & MongoDB 🔋
JavaScript
151
star
36

stream-chat-js

JS / Browser Client - Build Chat with GetStream.io
TypeScript
149
star
37

meeting-room-compose

🎙️ A real-time meeting room app built with Jetpack Compose to demonstrate video communications.
Kotlin
141
star
38

stream-php

PHP Client - Build Activity Feeds & Streams with GetStream.io
PHP
139
star
39

stream-log

🛥 Stream Log is a lightweight and extensible logger library for Kotlin Multiplatform.
Kotlin
136
star
40

react-activity-feed

Stream React Activity Feed Components
TypeScript
136
star
41

stream-python

Python Client - Build Activity Feeds & Streams with GetStream.io
Python
136
star
42

flat-list-mvcp

"maintainVisibleContentPosition" prop support for Android react-native
TypeScript
133
star
43

stream-node-orm

NodeJS Client - Build Activity Feeds & Streams with GetStream.io
JavaScript
131
star
44

website-react-examples

TypeScript
122
star
45

stream-video-swift

SwiftUI Video SDK ➡️ Stream Video 📹
Swift
117
star
46

react-native-samples

A collection of sample apps built using GetStream and React Native
JavaScript
116
star
47

flutter-instagram-clone

An Instagram clone using Flutter and Stream Feeds
Dart
111
star
48

django_twitter

An example app built using getstream.io
Python
108
star
49

twitter-clone

Learn how to build a functional Twitter clone using Stream, 100ms, Algolia, RevenueCat and Mux 😎
Swift
107
star
50

accessible-inclusive-ios-animations

Provide ways to limit animations/motion people find jarring in your apps. This repo demonstrates accessible and inclusive iOS animations/motion with practical examples and best practices.
Swift
107
star
51

stream-result

🚊 Railway-oriented library to easily model and handle success/failure for Kotlin Multiplatform.
Kotlin
102
star
52

Stream-Example-Nodejs

An example app built using getstream.io
JavaScript
96
star
53

Android-Video-Samples

📘 Provides a collection of samples that utilize modern Android tech stacks and Stream Video SDK for Kotlin and Compose.
Kotlin
88
star
54

react-native-audio-player

JavaScript
86
star
55

stream-ruby

Ruby Client - Build Activity Feeds & Streams with GetStream.io
Ruby
83
star
56

stream-tutorial-projects

This repo contains SwiftUI, Jetpack Compose, JS & React Native projects for some of the iOS and Android tutorial series in the Stream Developers YouTube channel (https://youtube.com/playlist?list=PLNBhvhkAJG6tJYnY-5oZ1JCp2fBNbVL_6).
Swift
83
star
57

stream-chat-unity

💬 Unity Chat Plugin by Stream ➜ These assets are the solution for adding an in-game text chat system to your Unity game.
C#
83
star
58

stream-go2

GetStream.io Go client
Go
82
star
59

stream-cli

Configure & manage Stream applications from the command line. 🚀
Go
80
star
60

mongodb-activity-feed

Activity Feed, Timeline, News Feed, Notification Feed with MongoDB, Node and CRDTs
JavaScript
77
star
61

android-video-chat

⚡️ Android Video Chat demonstrates a real-time video chat application by utilizing Stream Chat & Video SDKs.
Kotlin
76
star
62

TinyGraphQL

🌸 Simple and lightweight GraphQL query builder for the Swift language - Made with 💘 by @GetStream
Swift
76
star
63

slack-clone-expo

Slack clone using Expo, Stream and react-navigation
JavaScript
75
star
64

liveshopping-android

📹 A demo app showcasing real-time livestreaming and messaging capabilities built with Jetpack Compose and Stream SDKs.
Kotlin
74
star
65

fullstack-nextjs-whatsapp-clone

A sample codebase showcasing Stream Chat and Video to resembling WhatsApp, using NextJS, TailwindCSS and Vercel.
TypeScript
72
star
66

stream-feed-flutter

Stream Feed official Flutter SDK. Build your own feed experience using Dart and Flutter.
Dart
69
star
67

stream-chat-go

Stream Chat official Golang API Client
Go
67
star
68

stream-video-js

GetStream JavaScript Video SDK
TypeScript
66
star
69

discord-clone-nextjs

Building a discord clone using NextJS, TailwindCSS, and the Stream Chat and Audio and Video SDKs.
TypeScript
64
star
70

SwiftUIMessagesUIClone

The SwiftUI Messages Clone consists of layout and composition clones of the iOS Messages app. It has Messages-like bubble and screen effects, reactions, and animations, all created with SwiftUI.
Swift
62
star
71

Stream-Example-Py

An example app built using getstream.io
CSS
61
star
72

encrypted-web-chat

A web chat application end-to-end encrypted with the Web Crypto API
JavaScript
59
star
73

swift-lambda

λ Write HTTP services in Swift, deploy in seconds - Powered by AWS Lambda Runtime & Serverless Framework - Made with 💘 by @GetStream
Swift
57
star
74

stream-chat-python

Stream Chat official Python API Client
Python
56
star
75

stream-java

Java Client - Build Activity Feeds & Streams with GetStream.io
Java
53
star
76

stream-video-flutter

Flutter Video SDK - Build your own video app experience using Dart, Flutter and the Stream Video Messaging API.
Dart
46
star
77

Stream-Example-Parse

Stream-Example-Parse
JavaScript
46
star
78

Stream-Example-PHP

An example app built using getstream.io https://getstream.io
PHP
46
star
79

android-chat-tutorial

Sample apps for the Stream Chat Android SDK's official tutorial
Java
46
star
80

stream-meteor

Meteor Client - Build Activity Feeds & Streams with GetStream.io
JavaScript
45
star
81

SwiftUIChristmasTree

🌲 Pure SwiftUI christmas tree with yearly updates. Enjoy 🎄
Swift
45
star
82

python-chat-example

Chat with Python, Django and React
JavaScript
44
star
83

stream-chat-net

Stream Chat official .NET API Client
C#
43
star
84

stream-chat-dart

Dart SDK - Build Chat with GetStream.io
Dart
42
star
85

kmp-sample-template

A minimal Kotlin & Compose Multiplatform template designed to build applications for both Android and iOS.
Kotlin
41
star
86

Stream-Example-Go-Cassandra-API

Go-powered API example using Cassandra
Go
38
star
87

Stream-Example-Rails

An example app built using getstream.io
Ruby
38
star
88

Stream-Example-Android

Java
38
star
89

foldable-chat-android

🦚 Foldable chat Android demonstrates adaptive and responsive UIs with Jetpack WindowManager API.
Kotlin
35
star
90

stream-net

NET Client - Build Activity Feeds & Streams with GetStream.io
C#
35
star
91

stream-swift

Swift client for Stream API
Swift
35
star
92

stream-chat-angular

💬 Angular Chat SDK ➜ Stream Chat. Build a chat app with ease.
TypeScript
34
star
93

SwiftUI-open-voip-animations

SwiftUI animations and UI designs for iOS calling, meeting, audio-room, and live streaming use cases. Find something missing? Let @amos_gyamfi know on Twitter.
33
star
94

node-restify-mongo-api

Starter project for a REST API with Node.js, Restify & MongoDB 🔋
JavaScript
32
star
95

stream-chat-unreal

The official Unreal SDK for Stream Chat
C++
32
star
96

build-viking-sample

Sample app for Build Viking.
Dart
31
star
97

swiftui-iMessage-clone

Swift
30
star
98

swift-activity-feed

Stream Swift iOS Activity Feed Components
Swift
29
star
99

stream-chat-ruby

Stream Chat official Ruby API Client
Ruby
29
star
100

sign-in-with-apple-swift-example

iOS + Node.js authentication using Sign in with Apple
Swift
28
star