0

I am a beginner with node.js and API's, I followed the getstream.io tutorials and came up with the below code

Would the below code send a notification from user1 to user2?

And how do I confirm that this successfully happens?

var stream = require('getstream');
// Instantiate a new client (server side)
client = stream.connect('key', 'secret', '25553');
// creates token so user1 can read and write
var token = client.feed('user', '1').token;
var user1 = client.feed('user', '1', token);

// Create a bit more complex activity
activity = {
    'name': 'Jack', 
    'location': {'type': 'point', 'coordinates': [37.769722,-122.476944]},
    'to' : ['notification:user2'] 
};

user1.addActivity(activity)
    .then(function(data) { /* on success */ })
    .catch(function(reason) { /* on failure */ });

// update the buyer status value for the activity
activity.name = 'James';

// send the update to the APIs
client.updateActivities([activity]);

//client side
//creates new client for reading
var client2 = stream.connect('key', null, '25553'); 

//creates read only token from sever client
var readonlyToken = client.feed('user', '1').getReadOnlyToken();

//user 2 gets read only token from user 1
var user2 = client2.feed('user', '1', readonlyToken);

//notification seen
user2.get({limit:5, mark_seen:true})
    .then(function(data) { /* on success */ })
    .catch(function(reason) { /* on failure */ });

1 Answers1

0

If this code is all on the back end then you don't need to generate the tokens. You only need the tokens to pass to the front-end client.

For your client-side code you don't want this line:

var readonlyToken = client.feed('user', '1').getReadOnlyToken();

As mentioned in other posts, you generate that read-only token on the back end, pass it to the front end, and it's only good for websocket connections to Stream, of which you have a very limited supply (only 500 connections on our free plan) so generally we recommend that your back end pulls the feeds and sends the activity data to the front end.

Finally, the mark_seen and mark_read flags are used on notification feeds only, and your code is trying to use it on your user feed which is a flat feed.

iandouglas
  • 4,146
  • 2
  • 33
  • 33
  • Thank you! Just wondering, how can I pass the token from the back end to the front end? – Davidson Otobo Jun 10 '17 at 19:09
  • And how can I see the data sent over the API? – Davidson Otobo Jun 10 '17 at 19:22
  • The back end could respond to a GET from the front end, and send back the token in the payload. As for seeing data in the API, you can use the Explorer on our dashboard to see your feeds and the data within them, you can see follow relationships, etc., too. – iandouglas Jun 12 '17 at 16:26