Re: How do I change the tracking keyword for a twitter stream using the app.get()

31 views
Skip to first unread message

Aryak Sengupta

unread,
Mar 3, 2015, 11:45:22 AM3/3/15
to nod...@googlegroups.com
Any kind of help would be appreciated. I need to know whether I am moving in the right direction. Is it the way to do it? Or am I doing something terribly wrong. If my question needs further clarification, then please let me know. I'll try to phrase my question in a different manner. 

Thanks :) 

On Tue, Mar 3, 2015 at 12:59 PM, Aryak Sengupta <aryaks...@gmail.com> wrote:
Hi everyone, 

I am new to Nodejs and Socket.io. I have worked through a tutorial which describes how both the technologies work together. The link to the tutorial is this :--- >  Build a real-time Twitter stream 

and the code 
for the tutorial is posted at github :---> Code of react-tweets

Everything works fine in the tutorial and I also have no issues in understanding the code since the code is well commented.

 In the server.js file, the tracking keyword which is "javascript" is manually fed into the "twit" object. i.e. here is the code for it. 

twit.stream('statuses/filter',{ track: 'javascript'}, function(stream){
  streamHandler(stream,io);
});

But I want to make a minor alteration in the code to make the application a bit more versatile. I want to change the tracking keyword dynamically using the URL entered by the user.

For example, if the URL entered is http://localhost/football, then automatically I want the track keyword to be changed to "football". The code should look like 

twit.stream('statuses/filter',{ track: 'football'}, function(stream){
  streamHandler(stream,io);
});

Now, I tried to set this object inside the app.get("/cricket", twit.stream.......); function but I get an error "Expected callback got object" . 

How do I change the above mentioned code to achieve this?. Please let me know. 

Thanks a lot. 


Aryak Sengupta

unread,
Mar 3, 2015, 11:46:01 AM3/3/15
to nod...@googlegroups.com

zladuric

unread,
Mar 5, 2015, 9:47:50 AM3/5/15
to nod...@googlegroups.com
Hi,

This is going to be a two-step process for you. Your app now does two things:
1. Track tweets and save them to DB.
2. On rendering Index page, displays those tweets.

You want to change the following:
- change the tracking keyword based on url
- fetch only such keyworded tweets.

The first part is simple.

Instead of the part:

app.get('/', routes.index), you will have app.get('/:keyword', routes.keywordIndex);

And the routes.keywordIndex is a function that starts loading only functions by that keyword
You will have to alter the tweet model slightly:
- add a 'keyword' property (and save it from that nTwitter stream, along with tweet data).
- add a keyword parameter to Tweet.getTweets that will only search such tweets. (And take it into account with paging.

Then alter the bottom part of server.js - instead of starting to fetch right away, put the twit.stream(...) in a function that will take a keyword. It will only start running (fetching stream) as you call this function from route handler.

I think that nTwitter can only be used for one stream given one API key, but not sure on that. Check it yourself, and if it's true, this will be a single-user app :)

Here, I've given you one possible plan, but it's on you to start hacking into details, and hit stackoverflow when stuck.
Reply all
Reply to author
Forward
0 new messages