Posted in by Tudipat 08:06Comments Off on Implement Getstream.io with STI in Rails
Implement Getstream.io with STI in Rails
17 June 2021
What is Getstream.io?
What is the best way to interact with the live data in the application? Yes, the answer is through FEEDS or CHAT.
FEEDS or CHAT are the best way to be in touch with the people you care about.
However, the user expects the feeds to be more Relevant, Engaging and Fast.
Getting the relevant posts for a user by building a relational database is a lot more time consuming and difficult to maintain for large applications.
Gestream Activity Feeds is the best solution for building applications like Facebook, Instagram and Twitter.
Basically, any requirement where the user can follow things or people to get the best contents in which they are interested in is the best-fit Getstream.io
Getstream allows users to follow the topics or users of their interest and get the posts from them on their timeline.
What is STI – Single Table Inheritance?
When there is a case where you want to store the data into a single table but should have multiple subclasses sharing a single base class, then STI is the best fit for you.
In short, many subclasses inherit from a single base class storing the data in a single table. This is achieved with the help of the “Type” column in the base class.
We will have to add a type column in the base class, which tells rails that we are using STI. Any common validation we want to apply will be in the base class and the specific validations will be in a required subclass.
Implementing Getstream.io with STI
Let’s take a real-world example, where a user can create a post that can include an image, video, blog, audio etc. and all the posts will be stored in a single table called posts.
To achieve this, we will start with implementing STI for multiple types of posts and Getstream.io for getting the user feeds on the timeline.
First, we will start with creating a posts model and adding a type column to the posts table.
Our post model will look like this:
Then we will create some subclasses for other types of posts like image, video, audio.
The post table has a ‘type’ column and others are the subclasses derived from the post base class. Any common validation that we want to apply on post will be in post.rb and other post type-specific validations will be in the respective model.
We will have separate controllers and routes for creating all types of posts. You can define the routes like this:
We are all set with our STI structure and now the posts table will have a column type that will indicate the type of post. This will happen when we insert data into the posts table using Photo.new() or Video.new or Audio.new()
This way the type column will have values like “Photo” or “Video” or “Audio” depending on the type of post we create.
Here we are able to create three types of posts having different models derived from a single base class i.e post.rb
In the next step, we will implement the Getstream.io to get the feeds using the Stream Rails gem.
First, we will have to follow the steps given in the link below this link.
Install the stream_rails gem:
gem install stream_rails
Setting up the Getstream: Add a new file at config/initializers/stream_rails.rb with following content: To get the api key and secret key, we will have to log in to https://getstream.io/ and get the credentials.
Model configuration: Now as we are using STI for our posts, we have our base class called post.rb and instead of adding each post type to the stream, we will integrate our base class i.e post.rb with the stream.Add following code to post.rb
Our post model will look like this:
Now whenever we create a post, it will get stored into the feeds for that user and will be deleted from feeds once the post is deleted.
Now we are all set with integrating Getstream.io with our STI implementation.
There are few important concepts of Activity feeds:
Activity_object: The object of ActiveRecord model instance stored into the feed.
Activity_actor: The user who created that post.
Activity_verb: Model class name of the verb i.e Photo/Video/Audio.
We are able to insert into feeds whenever the post is created. But what about getting them back and displaying them on the user’s timeline?
Stream_rails provides the Feed Manager using which we can fetch the activities from the feeds.
There are various types of feeds present in the Feed Manager.
Stores all the activities of the current
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.