Skip to content

Instantly share code, notes, and snippets.

@tbarbugli
Created March 5, 2015 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbarbugli/afdf38d42bee5330b184 to your computer and use it in GitHub Desktop.
Save tbarbugli/afdf38d42bee5330b184 to your computer and use it in GitHub Desktop.
notification feed tutorial

Build a notification feed with Stream

Introduction

In this tutorial we are going to show how easy it is to add a notification feed using GetStream.io. If you are not familiar with GetStream.io don't worry, we will explain it later on :)

First of all, let's introduce quickly our fictional example app, let' call it bug-your-friends.com. This app allows you interact with your friends, ping them, follow them or poke them. Here's a quick list of example interactions:

poke another user (eg. Thierry pokes Alessandra) follow a user (eg. Tommaso follows Iris) ping a user (eg. Josie pings Carolina)

Whenever a user is part of one of these interactions, we want to update his own notification feed, update the number of unseen and unread notifications and do it realtime. We also want to present these interactions in an aggregated fashion, so that common events are grouped together (eg. Thierry poked you 100 times, Tommaso, Josie and 3 other people pinged you, ...).

GetStream.io

GetStream.io helps you build scalable newsfeeds and activity stream AND aggregated and notification feeds. Users of getstream use its APIs to interact with the feeds. As of today there are 4 official clients (PHP,JS,Ruby,Python) that one can use to connect to the APIs.

To start, you need to sign up on getstream.io and obtain an API key pair (key and secret), this is completely free and takes just a few minutes. Once you have an account, you need to install the client. (in this tutorial we are going to use Python)

pip install stream-python

Sending notifications

The first bit to add to our annoying app, is to actually send events to users' notification feeds. Here's is how you send a poke event.

import stream
client = stream.connect('api_key', 'api_secret')


def notify_ping(user, ping, ping_target):
    activity = {'actor': user.username, 'verb': 'ping', 'object': ping.id}
    feed = client.feed('notification', ping_target.id)
    feed.add_activity(activity)

The code explains itself pretty well, we first instanciate an instance of the api client (using api key and secret), then we compose the ping event as an activity (actor, verb, object) and then we send that data to the notification feed of the pinged user.

Read a notification feed

Reading a notification feed is even easier. Let's assume we have the client available in the scope.

feed = client.feed('notification', current_user.id)
activities = feed.get()['results']

activities is a python dictionary containing the following information: * the list of activities present in the feed * the count of activities that are yet to be marked as seen * the count of activities that are yet to be marked as read

Update seen and read notification counters

Every notification feeds keeps count of the notifications that are yet to be marked as read and seen. Stream's APIs allows you to mark the entire feed as read or seen as well as marking single activities as such.

feed = client.feed('notification', current_user.id)

# marks the entire feed as seen
feed.get(seen=True)

# marks the entire feed as read
feed.get(read=True)

# marks one activity as seen and read
activity_id = activity['id']
feed.get(read=[activity_id], seen=[activity_id])

Aggregation

As we introduced briefly in the beginning, activities sent to feeds get aggregated into groups. Let's expand a bit on this as this.

Stream's notifications feeds use user defined aggregation rules. Whenever an activity is added to a feed, a group id will be assigned as the result of the aggregated function. The default aggregation rule groups activities with verb and creation date (with day resolution) in common. Stream allows you to use different and even more complex aggregation rules based on the activity data.

BONUS ROUND: Realtime notifications

Push notifications are a great addition to a notification feed. Like the rest of the implementation, adding this with GetStream is trivial. It is possible to subscribes to feed updates using the javascript client (available on npm and github). This is how you can get notification updates. Whenever something is added to the current user's feed, the updateNotificationCallback function will be fired.

<script src="path/to/stream.js" />

<script>
require 'stream'
client = stream.connect('api_secret', null, '733');
notification = client.feed('notification', '{{ current_user.id }}');

function updateNotificationCallback(data){
    // new activities will be available in the data argument
}

notification.subscribe(function callback(data) {
    updateNotificationCallback(data);
});
</script>

Conclusion

Implementing a notification feed is a complex task, building it as a scalable system requires experience and a solid infrastructure. In this tutorial we showed how easy it is to implement this features using Stream's client. To keep this tutorial brief we only scratched the surface of the feature set that Stream offers. To have a better understanding I suggest you to try the 1 minute interactive demo (https://getstream.io/getstarted/) and to have a look at the code examples (https://getstream.io/docs/)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment