-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
89 lines (75 loc) · 2.44 KB
/
server.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
//OUR requirements for our node server
var util = require('util'),
http = require('http'),
express = require('express'),
ejs = require('ejs'),
app = express(),
Instagram = require('instagram-node-lib');
//Listen on port 3000
var port = process.env.PORT || 3000;
var server = app.listen(port);
var io = require('socket.io').listen(server);
//This is required to work on Heroku; it defaults to long polling; actual web-sockets not supported
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
//Our private instagram data, Create an App with Instagram Developers to get this info....
Instagram.set('client_id', '');
Instagram.set('client_secret', '');
Instagram.set('callback_url', '');
//Express template rendering...
app.use(express.static(__dirname + '/app'));
app.set('views', __dirname + '/ejs/views/');
app.engine('html', require('ejs').renderFile);
app.use(express.logger());
//Our function to handle the photo's from the subscription information
//we recieve. We only pull the latest [0] photo from the group of data
var getPhoto = function (inf){
inf = JSON.parse(inf);
prt = inf[0];
Instagram.geographies.recent({
geography_id: prt.object_id,
complete: function(data){
if(data[0] == null){
}else{
var piece = {};
piece.img = data[0].images.low_resolution.url;
piece.url = data[0].link;
io.sockets.emit('alert', prt.object_id);
io.sockets.emit('photo', piece);
}
}
});
}
//Get init sets our first set of images so the page doesn't load blank. it pulls from my specified location.
function getInit(){
Instagram.geographies.recent({
geography_id: 4251649,
complete: function(data){
if(data[0] == null){
}else{
data.forEach(function (pic) {
var piece = {};
piece.img = pic.images.low_resolution.url;
piece.url = pic.link;
io.sockets.emit('alert', piece.url);
io.sockets.emit('photo', piece);
});
}
io.sockets.emit('alert', "init Fired");
}
});
}
require('./config/routes')(app, Instagram, io, getPhoto, getInit);
//Connection for specific user, functions inside connection relate to individual users...
io.sockets.on('connection', function (socket) {
io.sockets.emit('alert', "connected");
//Fire getInit for each new connection
getInit();
//Disconnection....
socket.on('disconnect', function () {
});
//Recieving Data...
socket.on('my other event', function (data) { });
});