Created
April 25, 2018 09:27
-
-
Save m4n1ok/b7ad1efe6ba425bfffe2f23caefb454a to your computer and use it in GitHub Desktop.
Simple node server with socket.io configuration and tweet stream api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
twitter: { | |
consumer_key: '', | |
consumer_secret: '', | |
token: '', | |
token_secret: '' | |
}, | |
tracking: { | |
dev: ['gobelinou', 'taratta'], | |
prod: ['gobelinou'] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "server", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"engines": { | |
"node": "8.9.4" | |
}, | |
"scripts": { | |
"dev": "NODE_DEV=true node server.js", | |
"start": "node server.js" | |
}, | |
"author": "les-sauveteurs.com", | |
"license": "ISC", | |
"dependencies": { | |
"bufferutil": "3.0.3", | |
"express": "4.16.2", | |
"node-tweet-stream": "^2.0.3", | |
"utf-8-validate": "4.0.0", | |
"socket.io": "^2.0.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const config = require('./config') | |
const express = require('express') | |
const path = require('path') | |
const socketIO = require('socket.io') | |
const Twitter = require('node-tweet-stream')(config.twitter) | |
const PORT = process.env.PORT || 3000 | |
const DEV = process.env.NODE_DEV | |
const Trackers = DEV ? config.tracking.dev : config.tracking.prod | |
const INDEX = path.join(__dirname, 'index.html') | |
const server = express() | |
.use((req, res) => res.sendFile(INDEX)) | |
.listen(PORT, () => console.log(`Listening on ${ PORT }`)) | |
const io = socketIO(server) | |
io.on('connection', (socket) => { | |
console.log('Client connected'); | |
socket.on('disconnect', () => console.log('Client disconnected')); | |
}); | |
// Twitter configure | |
Twitter.on('tweet', function (tweet) { | |
console.log('tweet received', tweet) | |
const parseTweet = { | |
id: tweet.id, | |
user: { | |
id: tweet.user.id, | |
name: tweet.user.name, | |
location: tweet.user.location, | |
}, | |
text: tweet.text, | |
timestamp: tweet.timestamp, | |
hashtags: tweet.entities.hashtags | |
} | |
io.emit('tweet', parseTweet) | |
console.log(parseTweet) | |
}) | |
Twitter.on('error', function (err) { | |
console.log('Oh no') | |
}) | |
Trackers.forEach(function (tracker) { | |
console.log('tracker', tracker) | |
Twitter.track(tracker) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment