Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

simple-feed

> 编程语言
Open source

This gem implements a flexible time-ordered activity feeds commonly used within social networking applications. As events occur, they are pushed into the Feed a

336 stars0 likes1 views
WebsiteGitHub

About

This gem implements a flexible time-ordered activity feeds commonly used within social networking applications. As events occur, they are pushed into the Feed a

= SimpleFeed :doctype: book :toc: :toclevels: 5 :sectnums: :icons: font

====

https://liberapay.com/kigster/donate[image:https://liberapay.com/assets/widgets/donate.svg[Donate using Liberapay,height=30]]

https://liberapay.com/kigster/donate[image:https://img.shields.io/liberapay/goal/kigster.svg?logo=liberapay[https://img.shields.io/liberapay/goal/kigster,height=50]]

== Scalable, Easy to Use Activity (aka "News") Feed

=== What is this gem? — A Quick Summary

NOTE: This gem is the Redis-backed backend implementation for a reverse chronological order news feed for a typical social network similar in some ways to Twitter: where users have followers, but also topics or a categories can have followers. When an event is posted by a user, it is also pushed to various individual feeds, and the gem then offers O(1) speed pagination for reading any page of the feed. The limitation is that the number of feed items must be capped (the default is 4000 items). The implementation can be scaled horizontally by sharding Redis backend, which can be implemented using a Redis Proxy layer such as https://github.com/twitter/twemproxy[Twemproxy] or https://github.com/envoyproxy/envoy/issues/19436[Envoy]).

=== Build & Gem Status

image:https://img.shields.io/badge/license-MIT-blue.svg[MIT licensed,link=https://github.com/kigster/simple-feed/main/LICENSE.txt] image:https://github.com/kigster/simple-feed/actions/workflows/ruby.yml/badge.svg[RSpec,link=https://github.com/kigster/simple-feed/actions/workflows/ruby.yml] image:https://github.com/kigster/simple-feed/actions/workflows/rubocop.yml/badge.svg[Rubocop,link=https://github.com/kigster/simple-feed/actions/workflows/rubocop.yml]

image:https://img.shields.io/gem/v/simple-feed.svg[Gem Version,link=https://rubygems.org/gems/simple-feed] image:https://codecov.io/gh/kigster/simple-feed/branch/master/graph/badge.svg?token=RvDkGqoahz[Coverage,link=https://codecov.io/gh/kigster/simple-feed]

=== Test Coverage Map

image:https://codecov.io/gh/kigster/simple-feed/graphs/sunburst.svg?token=RvDkGqoahz[Coverage Map,link=https://codecov.io/gh/kigster/simple-feed/branch/master]

IMPORTANT: Please read the (somewhat outdated) blog post http://kig.re/2017/02/19/feeding-frenzy-with-simple-feed-activity-feed-ruby-gem.html[Feeding Frenzy with SimpleFeed] launching this library. Please leave comments or questions in the discussion thread at the bottom of that post. Thanks!

If you like to see this project grow, your donation of any amount is much appreciated.

image::https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif[Donate,link=https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=FSFYYNEQ8RKWU]

This is a fast, pure-ruby implementation of an activity feed concept commonly used in social networking applications. The implementation is optimized for read-time performance and high concurrency (lots of users), and can be extended with custom backend providers. One data provider come bundled: the production-ready Redis provider.

Important Notes and Acknowledgements:

  • SimpleFeed does not depend on Ruby on Rails and is a pure-ruby implementation
  • SimpleFeed requires MRI Ruby 2.3 or later
  • SimpleFeed is currently live in production
  • SimpleFeed can use https://github.com/redis/hiredis[hiredis] if you install it separately and load it before loading the gem.
  • SimpleFeed is open source thanks to the generosity of http://simbi.com[Simbi, Inc].

== Features

SimpleFeed is a Ruby Library that can be plugged into any application to power a fast, Redis-based activity feed implementation so common on social networking sites. SimpleFeed offers the following features:

  • Modelled after graph-relationships similar to those on Twitter (bi-directional independent follow relationships): ** Feed maintains a reverse-chronological order for heterogeneous events for each user. ** It offers a constant time lookup for user's feed, avoiding complex SQL joins to render it. ** An API to read/paginate the feed for a given user ** As well as to query the total unread items in the feed since it was last read by the user (typically shown on App icons).
  • Scalable and well performing Redis-based activity feed — ** Scales to millions of users (will need to use Twemproxy to shard across several Redis instances) ** Stores a fixed number of events for each unique "user" — the default is 1000. When the feed reaches 1001 events, the oldest event is offloaded from the activity.
  • Implementation properties: ** Fully thread-safe implementation, writing events can be done in eg. Sidekiq. ** Zero assumptions about what you are storing: the "data" is just a string. Serialize it with JSON, Marshall, YAML, or whatever. ** You can create as many different types of feeds per application as you like (no Ruby Singletons used). ** Customize mapping from user_id to the activity id based on your business logic (more on this later).

=== Publishing Events

Pushing events to the feed requires the following:

  • An Event consisting of: ** String data that, most commonly, is a foreign key to a database table, but can really be anything you like. ** Float at (typically, the timestamp, but can be any float number)
  • One or more user IDs, or event consumers: basically — who should see the event being published in their feed.

You publish an event by choosing a set of users whose feed should be updated. For example, were you re-implementing Twitter, your array of user_ids when publishing an event would be all followers of the Tweet's author. While the data would probably be the Tweet ID.

NOTE: Publishing an event to the feeds of N users is roughly a O(N * log(N)) operation

=== Consuming Events (Reading / Rendering the Feed)

You can fetch the chronologically ordered events for a particular user, using:

  • Methods on the activity such as paginate, fetch.

** Reading feed for one user (or one type of user) is a O(1) operation

  • For each activity (user) you can fetch the total_count and the unread_count — the number of total and new items in the feed, where unread_count is computed since the user last reset their read status.

** Note: total_count can never exceed the maximum size of the feed that you configured. The default is 1000 items.

** The last_read timestamp can be automatically reset when the user is shown the feed via paginate method (whether or not its reset is controlled via a method argument).

=== Modifying User's Feed

For any given user, you can:

  • Wipe their feed with wipe

  • Selectively remove items from the feed with delete_if. ** For instance, if a user un-follows someone they shouldn't see their events anymore, so you'd have to call delete_if and remove any events published by the unfollowed user.

=== Aggregating Events

This is a feature planned for future versions.

Help us much appreciated, even if you are not a developer, but have a clear idea about how it should work.

== Commercial & Enterprise Support

Commercial Support plans are available for SimpleFeed through author's https://reinvent.one[ReinventONE Inc] consulting company. Please reach out to kig AT reinvent.one for more information.

== Usage

=== Example

Please read the additional documentation, including the examples, on the https://github.com/kigster/simple-feed/wiki[project's Github Wiki].

Below is a screen shot of an actual activity feed powered by this library.

image::https://raw.githubusercontent.com/kigster/simple-feed/main/man/activity-feed-action.png[usage]

=== Providers

A key concept to understanding SimpleFeed gem, is that of a provider, which is effectively a persistence implementation for the events belonging to each user.

One providers are supplied with this gem: the production-ready :redis provider, which uses the https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/chapter-3-commands-in-redis/3-5-sorted-sets[sorted set Redis data type] to store and fetch the events, scored by time (but not necessarily).

You initialize a provider by using the SimpleFeed.provider([Symbol]) method.

=== Configuration

Below we configure a feed called :newsfeed, which in this example will be populated with the various events coming from the followers.

[source,ruby]

require 'simplefeed'

Let's define a Redis-based feed, and wrap Redis in a in a ConnectionPool.

SimpleFeed.define(:newsfeed) do |f| f.provider = SimpleFeed.provider(:redis, redis: -> { ::Redis.new }, pool_size: 10) f.per_page = 50 # default page size f.batch_size = 10 # default batch size f.namespace = 'nf' # only needed if you use the same redis for more than one feed

end

After the feed is defined, the gem creates a similarly named method under the SimpleFeed namespace to access the feed. For example, given a name such as :newsfeed the following are all valid ways of accessing the feed:

  • SimpleFeed.newsfeed
  • SimpleFeed.get(:newsfeed)

You can also get a full list of currently defined feeds with SimpleFeed.feed_names method.

=== Reading from and writing to the feed

For the impatient, here is a quick way to get started with the SimpleFeed.

[source,ruby]

Let's use the feed we defined earlier and create activity for all followers of the current user

publish_activity = SimpleFeed.newsfeed.activity(@current_user.followers.map(&:id))

Store directly the value and the optional time stamp

publish_activity.store(value: 'hello', at: Time.now)

=> true # indicates that value 'hello' was not yet in the feed (all events must be unique)

Or, using the event form:

publish_activity.store(event: SimpleFeed::Event.new('good bye', Time.now))

=> true


As we've added the two events for these users, we can now read them back, sorted by the time and paginated:

[source,ruby]

Let's grab the first follower

user_activity = SimpleFeed.newsfeed.activity(@current_user.followers.first.id)

Now we can paginate the events, while resetting this user's last-read timestamp:

user_activity.paginate(page: 1, reset_last_read: true)

[

[0] #,

[1] #,

]


IMPORTANT: Note that we stored the activity by passing an array of users, but read the activity for just one user. This is how you'd use SimpleFeed most of the time, with the exception of the alternative mapping described below.

=== User IDs

In the previous section you saw the examples of publishing events to many feeds, and then reading the activity for a given user.

SimpleFeed supports user IDs that are either numeric (integer) or string-based (eg, UUID). Numeric IDs are best for simplest cases, and are the most compact. String keys offer the most flexibility.

==== Activity Keys

In the next section we'll talk about generating keys from user_ids. We mean — Redis Hash keys that uniquely map a user (or a set of users) to the activity feed they should see.

There are up to two keys that are computed depending on the situation:

  • data_key is used to store the actual feed events
  • meta_key is used to store user's last_read status

==== Partitioning Schema

NOTE: This feature is only available in SimpleFeed Version 3+.

You can take advantage of string user IDs for situations where your feed requires keys to be composite for instance. Just remember that SimpleFeed does not care about what's in your user ID, and even what you call "a user". It's convenient to think of the activities in terms of users, because typically each user has a unique feed that only they see.

But you can just as easily use zip code as the unique activity ID, and create one feed of events per geographical location, that all folks living in that zip code share. But what about other countries?

Now you use partitioning scheme: make the "user_id" argument a combination iso_country_code.postal_code, eg for San Francisco, you'd use us.94107, but for Australia yo

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言