We use cookies to personalize content and ads, to offer features for social media and to analyze the access to our website. We also share information about your use of our website with our social media, weaving and analytics partners. Our partners may combine this information with other information that you have provided to them or that they have collected as part of your use of the Services. You accept our cookies when you click "Allow cookies" and continue to use this website.

Allow cookies Privacy Policy

Add a Personalized Feed to Your Ruby on Rails Application (redis + sidekiq)

In this tutorial I want to show you how to implement a linkedin or twitter style feed in a RubyOnRails project.

Coding


Feeds are commonly used on social media platforms and in news applications and allow users or readers to see news from their friends, colleagues, or other sources of interest. Larger platforms also often use AI to populate users' feeds.

The feed can also help increase engagement on the site by making it easier for users to consume, create and interact with content.

However, there are also situations where a feed may not be the best choice. For example, if the application needs to display a large amount of data other UI elements such as tables or grids are better suited.

It's important to consider the specific needs and goals of your application before deciding if a feed is the right choice.

In this article, I will show you how to implement a personalized feed in your RubyOnRails application.

Since we are going to use redis and sidekiq in this example, you should make sure that you have the following gems installed in your Gemfile:

gem 'redis-rails'
gem 'sidekiq'

Run bundle install to install the gems.

Next, you'll need to set up a Redis server to store your feed data. You can do this by following the instructions here: https://redis.io/topics/quicks...

Once you have Redis set up, you'll need to set up Sidekiq to handle background processing. You can do this by following the instructions here: https://github.com/mperham/sid...

Now you're ready to start implementing the feed. The first step is to create a model for the feed items. A feed item is a user generated content, like a posting. This could be something like this:

class FeedItem < ApplicationRecord
  belongs_to :user
  # Add any other fields you need, such as content, tags, timestamps, etc.
end

We want to keep performance in line, so you will need to create a background worker to handle adding items to the feed. This worker will be responsible for taking a new feed item and adding it to the feed for each of the user's followers. You can do this with something like the following:

class AddToFeedWorker
 include Sidekiq::Worker
   def perform(feed_item_id)
     feed_item = FeedItem.find(feed_item_id)
     user = feed_item.user
     
     # Find all of the user's followers
     followers = user.followers
    
     # Add the feed item to each follower's feed
     followers.each do |follower|
       follower.feed << feed_item
     end
     
     # Or any other logic, like "user has intersection interests with the feed item"
  end
end

Now you can use this worker to add items to the feed whenever you need to. For example, if you have a form for creating new posts, you can do something like this:

def create
  # Create the new feed item
  feed_item = FeedItem.create(content: params[:content], user_id: current_user.id)
  
  # Add the feed item to the feeds of all the user's followers
  AddToFeedWorker.perform_async(feed_item.id)
   
  # Redirect to the feed page
  redirect_to feed_path
end

To display the feed on the front-end, you'll need to create a controller action to fetch the feed data. This action should use the current_user helper to fetch the feed items for the logged-in user. You can do this with something like the following:

def index
  # Fetch the feed items for the current user
  @feed_items = current_user.feed.order(created_at: :desc).limit(10)
end

Finally, you can display the feed items on the front-end using something like the following in your view:

<% @feed_items.each do |item| %>
  <%= item.content %>
<% end %>

This should give you a good start to implement a feed in your new or existing web application. Consider this is only a starter for your project. If you want to run a feed in production system you keep these things in mind:

  • Caching: To improve performance, you may want to consider caching the feed data. This can help to reduce the number of database queries required to fetch the feed data. You can use the already installed redis instance to store the feed data, and then use this data to populate the feed on the front-end.
  • Pagination: If the feed could potentially contain a large number of items, you may want to implement pagination to improve performance and usability. You can do this by using the kaminari gem, which provides a simple way to paginate Active Record collections.
  • Personalization: In the current implementation, the feed is only personalized based on the user's followers. You may want to further personalize the feed by taking into account the user's selected interests. This could be done by adding a many-to-many relationship between users and interests, and then using this data to filter the feed items.
  • Error handling: It's important to handle errors gracefully in a production application. You may want to add error handling to the AddToFeedWorker class to ensure that any errors that occur during background processing are logged and properly handled.

Thank for reading! Reach me out, if you have any questions!

Cheers,

Simon



Share this article:

zauberware logo