Sunday, January 11, 2015 Twitter Streaming API - Node.js Deployed on

We Would be curious About Chat Applications Response on time and blaming it if makes struggle a lot to connect with servers and message keeps on Failing with lot of Notification that "Message Failed".Many Of my Friends have faced that and Trolled the Application.And here comes the Technology known As Sockets/AJAX polling to instantly chat with our Besties/Crush :D .However we could use the Socket/Ajax Long polling Technology for Streaming Videos,Files and Tweets from Twitter for Web/Mobile and Desktop Apps too and for Real time can be also used for Collaborative works over online to maintain concurrency among the group members and real time!

Reference : Download | Live Demo

Ultimate Goal :

After Working/Reading this Post you can technically create and set up Node.js with Server side and connect with Clients and Stream Media/Text Whatever may be!

Prerequisite : 

1) Twitter Account with Verified by Mobile Number(This is mandatory to create apps on Dev.twitter)
4) Node.js npm Commands /basics 

Procedure :

Create a Twitter App :
  • Start Creating Twitter App - Link [ Callback not Required ]
  • Copy Consumer Key / Consumer Secret

  Setting Up Server : 

Node.js Server Setup with and Writing functions for events that Emit.Generally Node.js Event Driven Programming Language which has infinite loops to execute in single threaded.So just we are going to write some Routines for Functions which mean that they must Execute when corresponding event occurs.

 > npm install -g

>  npm install -g express

package.json :

"description":"Simple Streaming Twitter",

Install the package as per directed in package.json file.Additionally i have included twit which is also library acts for Twitter API.

server.js :

var express = require('express')
  , app = express()
  , http = require('http')
  , server = http.createServer(app)
  ,Twit = require('twit')
  , io = require('').listen(server);


  app.use(express.static(__dirname + '/public'));

 app.get('/', function (req, res) {

      res.sendFile(__dirname + '/index.html');

var target = ['cricket'];

 var api = new Twit({
    consumer_key:         'YYYYYYYYY'
  , consumer_secret:      'YYYYYYYYY'
  , access_token:         'YYYYYYYYYY'
  , access_token_secret:  'YYYYYYYYYY'

io.sockets.on('connection', function (socket) {

 var stream ='statuses/filter', { track: target })

  stream.on('tweet', function (tweet) {




The above code contains the twit,Http and Express and then we written function to be triggered when connection to client is successfully we are giving our required consumer key,Consumer Secret ,Access Token and Access Token Secret.with that we have added the streaming Watch list and then made API call to Twitter to get the Right Status and we are Emitting the Tweet as Data to our connected Clients via Sockets/Ajax Long Polling Approach.

Read here about How Does Works / Does uses Websockets ?

Client Setup :

The Basic Rule is simple Javascript event is triggered to connect to the server and Long Polling is done on Server to ensure they are connected and either Data gets Transmitted/Received using the Events by Javascript

A simple Client Example : 

<script src="/"></script>
   <script src=""></script>
        var socket = io.connect(); 
        socket.on('stream', function(tweet){

Resultant :

Final Word :

Thus We had Successfully created a simple Streaming from Twitter Real Time Updates with Node.js and you're Downloading the Code replace the Keys and Work it out in Localhost and test the Streaming API from twitter.However this is the basic part of could perform a lot when you go through the Documentation from Twitter!.

We will see how to Deploy Socket chat apps/Real time Data Sharing apps on Cloud in Next post.

hope you have Enjoyed!For bugs/comments/hugs/report/issue/help just drop me a mail [email protected].or connect with me in Facebook/Twitter or Chat with me In G+.share is care.


Post a Comment

feel free to post your comments! Don't Spam here!