-
Notifications
You must be signed in to change notification settings - Fork 5
/
server_accessing_tweets_at_runtime.js
128 lines (101 loc) · 3.62 KB
/
server_accessing_tweets_at_runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Becuase we will be reading some file we need file system module */
var fs = require("fs");
//Including twit npm module
var Twit = require('twit')
/* Log that we are starting */
console.log("Starting");
/* Using JSON config file to define host and port */
var config = JSON.parse(fs.readFileSync("config_host.json"));
var host = config.host;
var port = config.port;
/* Including express framework */
var express = require("express");
/* Creating server */
var app = express();
/* Specify individual paths and each path will have it's own callback */
/* Root path */
app.get("/",function (request,response){
//Reading contents of html file
var content = fs.readFileSync("template.html");
//get tweets
getTweets(function(tweets){
var ul = '';
//Creating an unordered list with tweets
tweets.forEach(function(tweet){
ul += '<li><strong>' + tweet.user.screen_name + ": </strong>" + tweet.text + "</li>";
});
//Assigning back to itself
content = content.toString("utf8").replace("{{INITIAL_TWEETS}}",ul);
//Set custom header
response.setHeader("Content-type","text/html");
response.send(content);
});
});
/* Similar to http callback */
//app.listen(port,host);
//EXPRESS API has been changed reference http://stackoverflow.com/questions/10191048/socket-io-js-not-found
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(port);
/* Above code working fine now socket.io working fine */
//using socket io with express web server
//var io = require('socket.io').listen(app);
//Including the namespace we still have to access classes
var mongo = require("mongodb");
//Choose our host and port
var dbHost = "127.0.0.1";
//Get port from Connection (listening information)
var dbPort = mongo.Connection.DEFAULT_PORT;
//Database variable which will store our connection details
//mongo is connection Db is the class and node-js-intro is name of database
//Then we need to pass the server to connect to then we will use mongo namespace and class called Server
var db = new mongo.Db("node-js-intro",new mongo.Server(dbHost,dbPort,{}) );
//Variable to store table name
var tweetCollection;
db.open(function(error){
console.log("We are connected to : " + host + ":" + port);
//Selecting a table
db.collection("tweet",function(error,collection){
tweetCollection = collection;
});
});
//function to get last 10 tweets
function getTweets(callback){
//Adding empty object {} as we want all the results
tweetCollection.find({},{"limit":10,"sort":{"_id":-1}},function(error,cursor){
cursor.toArray(function(error,tweets){
callback(tweets);
});
});
}
/* Code that connect with twitter and streams data into database */
//Getting tweets at runtime
//Refering to table name
var tweetCollection;
//Defining access tokens, Get this by creating new twitter application
var T = new Twit({
consumer_key: '...'
, consumer_secret: '...'
, access_token: '...'
, access_token_secret: '...'
})
//Creating a request variable and using stream function
var request = T.stream('statuses/filter', { track: 'bieber' })
//Printing tweets related to bieber on a screen
request.on('tweet', function (tweet) {
//Every client which is connected to our website is included in sockets
//emit is to send data to client,We supplied tweet(json) as js object
//Sockets will serialise tweet which is deserialised at client(template.html)
io.sockets.emit("tweet",tweet);
tweetCollection.insert(tweet,function(error){
if(error){
console.log(error);
}else{
console.log("Inserted into database");
}
});
console.log(tweet.text);
console.log("\n");
})
request.end();