Node

Targeting with TO Field

LAST EDIT Mar 10 2021

The TO field allows you to specify a list of feeds to which the activity should be copied. One way to think about it is as the CC functionality of email.

Use Case: Mentions

Copied!

Targeting is useful when you want to support @mentions and notify users. An example is shown below:

1
2
3
4
5
6
7
8
9
10
// Add the activity to Eric's feed and to Jessica's notification feed 
const activity = { 
	actor: 'user:Eric', 
	message: "@Jessica check out getstream.io it's awesome!", 
	verb: 'tweet', 
	object: 'tweet:id', 
	to: ['notification:Jessica'], 
}; 
 
const response = await user_feed_1.addActivity(activity)
1
2
3
4
5
6
7
8
9
# Add the activity to Eric's feed and to Jessica's notification feed 
data = { 
    :actor => "user:Eric", 
    :message => "@Jessica check out getstream.io it's totally awesome - totally.", 
    :verb => "tweet", 
    :object => "tweet:id", 
    :to => ["notification:Jessica", ] 
} 
user_feed_1.add_activity(data);
1
2
3
4
5
6
7
8
9
10
# Add the activity to Eric's feed and to Jessica's notification feed 
activity = { 
    "actor":"user:Eric", 
    "message": "@Jessica check out getstream.io it's so dang awesome.", 
    "verb":"tweet", 
    "object":"tweet:id", 
    "to":["notification:Jessica"] 
} 
user_feed_1.add_activity(activity) 
# In production use user ids, not their usernames
1
2
3
4
5
6
7
8
9
// Add the activity to Eric's feed and to Jessica's notification feed 
activity = Activity.builder() 
        .actor("User:Eric") 
        .verb("tweet") 
        .object("tweet:id") 
        .to(Lists.newArrayList(new FeedID("notification:Jessica"))) 
        .extraField("message", "@Jessica check out getstream.io it's so dang awesome.") 
        .build(); 
userFeed.addActivity(activity);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Add the activity to Eric's feed and to Jessica's notification feed 
activity := stream.Activity{ 
    Actor:  "user:Eric", 
    Verb:   "tweet", 
    Object: "tweet:id", 
    To: []string{ 
        "notification:Jessica", // In production use user ids, not their usernames 
    }, 
    Extra: map[string]interface{}{ 
        "message": "@Jessica check out getstream.io it's so dang awesome.", 
    }, 
} 
_, err := userFeed.AddActivity(activity) 
if err != nil { 
    panic(err) 
}
1
2
3
4
5
6
7
8
9
10
// Add the activity to Eric's feed and to Jessica's notification feed 
$data = [ 
    "actor"=>"user:Eric", 
    "message"=>"@Jessica check out getstream.io it's awesome!!!1", 
    "verb"=>"tweet", 
    "object"=>"tweet:id", 
    "to"=> ["notification:Jessica"], 
]; 
 
$userFeed1->addActivity($data);
1
2
3
4
5
6
7
8
9
// Add the activity to Eric's feed and to Jessica's notification feed 
var activity = new Activity("user:Eric", "tweet", "tweet:id") 
{ 
    To = new string[] { "notification:Jessica" } 
}; 
activity.SetData("message", "@Jessica check out getstream.io it's so dang awesome."); 
 
await userFeed1.AddActivity(activity); 
// In production use user ids, not their usernames
1
2
3
4
5
6
7
8
9
// Add the activity to Eric's feed and to Jessica's notification feed: 
let activity = TweetActivity(actor: "user:Eric",  
                             verb: "tweet",  
                             object: "tweet:id", 
                             feedIds: [FeedId(feedSlug: "notification", userId: "Jessica")], 
                             message: "@Jessica check out getstream.io it's awesome!") 
 
userFeed1.add(activity) { result in /* ... */ } 
// In production use user ids, not their usernames.
The TO field is limited to a maximum of 100 targets per API request.

Use Case: Organizations & Topics

Copied!

Another everyday use case is for teams, organizations or topics.

For example, one of our customers runs a football community. Updates about a player should also be added to their team's feed. Stream supports this by adding the activity to the player's feed, and specifying the target feeds in the TO field:

1
2
3
4
5
6
7
8
9
10
// The TO field ensures the activity is sent to the player, match and team feed 
const activity = { 
	actor: 'Player:Suarez', 
	verb: 'foul', 
	object: 'Player:Ramos', 
	match: { name: 'El Clasico', id: 10 }, 
	to: ['team:barcelona', 'match:1'], 
}; 
 
const response = await playerFeed1.addActivity(activity);
1
2
3
4
5
6
# The TO field ensures the activity is send to the player, match and team feed 
activity_data = {:actor => 'Player:Suarez', :verb => 'foul', :object => 'Player:Ramos', 
    :match => {:name => 'El Clasico', :id => 10}, 
    :to => ['team:barcelona', 'match:1'], 
} 
player_feed_1.add_activity(activity_data)
1
2
3
4
5
6
# The TO field ensures the activity is send to the player, match and team feed 
activity_data = {'actor': 'Player:Suarez', 'verb': 'foul', 'object': 'Player:Ramos', 
    'match': {'name': 'El Clasico', 'id': 10}, 
    'to': ['team:barcelona', 'match:1'] 
} 
player_feed_1.add_activity(activity_data)
1
2
3
4
5
6
7
8
9
// The TO field ensures the activity is send to the player, match and team feed 
activity = Activity.builder() 
        .actor("Player:Suarez") 
        .verb("foul") 
        .object("Player:Ramos") 
        .to(Lists.newArrayList(new FeedID("team:barcelona"), new FeedID("match:1"))) 
        .extraField("match", ImmutableMap.of("El Classico", 10)) 
        .build(); 
playerFeed.addActivity(activity).join();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// The TO field ensures the activity is send to the player, match and team feed 
activity := stream.Activity{ 
    Actor:  "Player:Suarez", 
    Verb:   "foul", 
    Object: "Player:Ramos", 
    To: []string{ 
        "team:barcelona", 
        "match:1", 
    }, 
    Extra: map[string]interface{}{ 
        "match": map[string]interface{}{ 
            "name": "El Clasico", 
            "id":   10, 
        }, 
    }, 
} 
_, err := playerFeed.AddActivity(activity) 
if err != nil { 
    panic(err) 
}
1
2
3
4
5
6
7
8
9
10
// The TO field ensures the activity is send to the player, match and team feed 
$data = [ 
    'actor' => 'Player:Suarez', 
    'verb' => 'foul', 
    'object' => 'Player:Ramos', 
    'match' => ['name' => 'El Clasico', 'id' => 10], 
    'to' => ['team:barcelona', 'match:1'], 
]; 
 
$playerFeed1->addActivity($data);
1
2
3
4
5
6
// The TO field ensures the activity is sent to the player, match and team feed 
var activity = new Activity("Player:Suarez", "foul", "Player:Ramos") 
{ 
    To = new string[] { "team:barcelona", "match:1" } 
}; 
await playerFeed1.AddActivity(activity);
1
2
3
4
5
6
7
8
9
// The TO field ensures the activity is send to the player, match and team feed 
let activity = MatchActivity(actor: "Player:Suarez", 
                             verb: "foul", 
                             object: "Player:Ramos", 
                             match: Match(name: "El Clasico", id: 10), 
                             feedIds: [FeedId(feedSlug: "team", userId: "barcelona"), 
                                       FeedId(feedSlug: "match", userId: "1")])  
 
playerFeed1.add(activity) { result in /* ... */ }
By default only the user, flat, aggregated and notification feed groups are set up automatically. You'll need to go to the dashboard and create feeds called team, player and match for the above example to work.