2

I wrote a piece of code that allows me search for all tweets hash tagged hello.

var stream = T.stream('statuses/filter', { track: 'hello', stall_warnings: true });
var counter = 0;

if (stream) {
console.log('connected!');
};


stream.on('tweet', function (tweet) {
    console.log('tweet: '+ tweet.text);
    console.log('by:' + ' @' + tweet.user.screen_name);
    console.log('date:'+ ' ' + tweet.created_at + ' | ' + counter);

counter++;
});

How do I go about redirecting this so that I can create a web page that looks like a Twitter stream data, or something of the sort? Maybe using AngularJS.

5
  • 3
    Look into web sockets (socket.io specifically for node) Commented Aug 31, 2015 at 13:42
  • Angular is also a great fit with socket.io - see github.com/btford/angular-socket-io Commented Aug 31, 2015 at 13:44
  • I am not understanding what you are trying to do exactly. maybe expand your post? Commented Aug 31, 2015 at 13:45
  • Here's an example of a realtime twitter feed app from FireBase. Its a great place to get started: github.com/firebase/firefeed. Demo: firefeed.io Commented Aug 31, 2015 at 13:45
  • I dont think a server hosting a bunch of servers and clients to the same IP would be the best solution to this. i would avoid using web sockets. Commented Aug 31, 2015 at 13:47

2 Answers 2

3

You will have to create a web server first, try express.

then you can use something like sockets.io to communicate from the server to your client web page.

then on the webpage you must handle the messages to display them (angular, or maybe just jQuery) - basically on tweet you will send a message from your server to the client web page through socket.io, then your dront end javascript will get the message, parse it and decide how to display it.

Have a look at Sails.js, it's basically express with sockets integrated and a few more things

edit

say you export your server in server.js,

var http = require('./server.js');
var io = require('socket.io')(http);

stream.on('tweet', function (tweet) {
  io.sockets.emit("new tweet", {
    text: tweet.text,
    by: tweet.user.screen_name,
    date: tweet.created_at,
    counter: counter++;
  });
});

require('socket.io')(http) starts the "socket manager" on your server (and also publishes the js client side code for it), so clients can connect to your server through sockets.

io.sockets.emit will send a message to all connected clients.

on your web page you must have something like this

<div id="tweets"></div>
<script src="/your/js/jquery.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
   var socket = io();
   socket.on("new tweet", function(tweet) {
       $('#tweets').append('tweet: ' + tweet.text + '<br>');
       $('#tweets').append('by:' + ' @' + tweet.by + '<br>');
       $('#tweets').append('date:'+ ' ' + tweet.date + ' | ' + tweet.counter + '<br>');
   });
</script>

the library /socket.io/socket.io.js was published by that require('socket.io')(http) from earlier, so we can use it on our clients.

the call io() basically connects to the server, and returns a handle to that connection (socket), we use that to receive all messages from the server, and on each message you can write the contents to the page anyway you want.

Sign up to request clarification or add additional context in comments.

5 Comments

I already have Express. Could you use the above answer as a format and tell me in code how that would look like?
So I wrote that code as you asked me to. After I run my server.js using node, how / where should I run the html page?
here's a very simple example expressjs.com/starter/hello-world.html to serve your page you'll need to maybe add the static middleware expressjs.com/starter/static-files.html
Yes, I understood how that works. But now, how do I integrate the server.js file with this example? Is the code in server.js supposed to be a part of the app.js file in link you provided above or a separate file? If so, how do I call it?
socket.io/docs the first thing on that page is an example with both client and server
0

With socket.io you can broadcast events from the server to the client, in this case you could do something like this :

stream.on('tweet', function (tweet) {
  io.sockets.emit("new tweet", tweet);
  counter++;
});

And you could receive that event on the client-side like this :

var socket = io();
socket.on("new tweet", function(tweet){
  //Do something with the tweet
});

This is a very basic and generic example, for more information you can look at the official documentation here.

2 Comments

Do I need to have a web server set up for this? Or Will just using this code work?
This code will work as long as you import the socket.io client-side library and that you install the module for your server. Everything is explained on their website. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.