0

My compiler returns errors saying I should pass in a key when I instantiate a new client, it says I should pass in a key when I instantiate a client.

I also get an error saying $token is not defined.

Please review the below code

var stream = require('getstream');
// Instantiate a new client (server side)
client = stream.connect('q3nwu4pbz222', '(secret)', '25553');
// Instantiate a new client (client side)
client = stream.connect('q3nwu4pbz222', null, '25553'); 
// Find your API keys here https://getstream.io/dashboard/

// Instantiate a feed using feed class 'user' and user id '1'
var user1 = client.feed('user', '1', token); 

// Instantiate a feed for feed group 'user', user id '1' and a security token generated server side
user1 = client.feed('user', '1', $token);
iandouglas
  • 4,146
  • 2
  • 33
  • 33
  • I edited your question to remove your API secret. You may want to generate a new API credential for your app. These credentials should be guarded the same as a username/password. – iandouglas Jun 08 '17 at 16:08

1 Answers1

0

JavaScript doesn't use $ prefixes for variables, but the example code you've given above also does not build a variable called token of any kind.

There are two client connections you can make with the stream-js library:

One happens in your Node.js application that runs on the server, and that one you would pass your API key and API secret. This gives you a read-write connection to Stream to read feeds but also to write activities, perform follow relationships, and to delete/update activities and unfollow other feeds, etc.

The other client would happen in the browser (where your API secret is null) but requires a token that gets generated by your back-end/server application and allows a read-only connection to Stream over websockets for real-time notifications. Your account has a limit on how many active websocket connections your browser clients can make to Stream.

As far as generating the token for the websocket connection, you would use code like this on the server-side:

backendClient = stream.connect(<API_KEY>, <API_SECRET>, <APP_ID>)
readonlyToken = backendClient.feed('user', '1').getReadOnlyToken();

This generates a read-only token for the user:1 feed but cannot perform write functions like adding/editing/updating activity data or follow/unfollows, etc..

Then, you'd get that token to your front-end browser/client however you like. From there, then, the front-end client would make a connection to Stream using code like this:

browserClient = stream.connect('q3nwu4pbz222', null, '25553'); 
user1 = browserClient.feed('user', '1', readonlyToken);
user1.get({limit:5}, callbackWhenFinished);
iandouglas
  • 4,146
  • 2
  • 33
  • 33
  • Does this happen in realtime? If not how can I get the browser Client to read in realtime? – Davidson Otobo Jun 08 '17 at 16:34
  • If you have a websocket connection from your browser to Stream with the proper read-only token then yes it's near real-time aside from internet latencies and our database processing of getting an activity and writing it into a feed that your websocket would query for new data. Our free plan has lower priority processing time so it can take longer than, say, our enterprise plans. – iandouglas Jun 09 '17 at 04:33