Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

socket.io funcionando y mongoose almacenando tweets

  • Loading branch information...
commit 5afe9de3a8bba7c932e95867c8e4c87063ee047c 1 parent 44830a3
@arpunk arpunk authored
Showing with 36 additions and 5 deletions.
  1. +31 −4 app.js
  2. +3 −1 views/index.jade
  3. +2 −0  views/layout.jade
View
35 app.js
@@ -11,6 +11,19 @@ var express = require('express'),
var app = module.exports = express.createServer();
+mongoose.connect('mongodb://localhost/ejemplo');
+
+// Model
+
+var Tweet = new mongoose.Schema({
+ tweet: String,
+ author: String,
+ id: String,
+ date: { type: Date, default: Date.now }
+});
+
+var TweetModel = mongoose.model('Tweet', Tweet);
+
// Configure credentials
var twit = new ntwitter({
@@ -46,18 +59,32 @@ app.get('/', routes.index);
twit.verifyCredentials(function(err, data) {
if(err) throw new Error(err);
- console.log(data);
});
-twit.stream('user', { track: "Iran" }, function(stream) {
+twit.stream('user', { track: "#30thingsaboutme" }, function(stream) {
stream.on('data', function(data) {
- console.log(data);
+ if(data.user) {
+ var _tweet = new TweetModel({
+ author: data.user.screen_name,
+ tweet: data.text,
+ id: data.id
+ });
+
+ _tweet.save(function(err, tweet) {
+ if(err) throw new Error(err);
+ io.sockets.emit('new tweet', { tweet: data.text, author: data.user.screen_name });
+ });
+ }
});
});
// Wrap socket.io in app
-io.listen(app);
+io = io.listen(app);
+
+io.sockets.on('connection', function(socket) {
+ console.log("Cliente conectado");
+});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
4 views/index.jade
@@ -1,2 +1,4 @@
h1= title
-p Welcome to #{title}
+p Welcome to #{title}
+
+#tweet
View
2  views/layout.jade
@@ -5,3 +5,5 @@ html
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
script(src='/socket.io/socket.io.js')
+ script(src='/javascripts/jquery-1.7.1.min.js')
+ script(src='/javascripts/main.js')
Please sign in to comment.
Something went wrong with that request. Please try again.