Created
January 31, 2014 10:53
-
-
Save vshjxyz/8729990 to your computer and use it in GitHub Desktop.
Node.js examples
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
http = require "http" | |
https = require "https" | |
### | |
getJSON: REST get request returning JSON object(s) | |
@param options: http options object | |
@param callback: callback to pass the results JSON object(s) back | |
### | |
exports.getJSON = (options, onResult) -> | |
console.log "---> #{options.method} ~ #{options.host}:#{options.port}#{options.path}" | |
prot = (if options.port is 443 then https else http) | |
req = prot.request(options, (res) -> | |
output = "" | |
console.log "<--- response code - #{res.statusCode}" | |
res.setEncoding "utf8" | |
res.on "data", (chunk) => | |
output += chunk | |
res.on "end", => | |
try | |
obj = JSON.parse output | |
catch e | |
console.error "Error during the JSON parsing, output: #{output}" | |
onResult res.statusCode, obj | |
) | |
req.on "error", (err) -> | |
console.error "Error during the rest call: #{err.message}" | |
req.end() |
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
# Node.js server-side script | |
# @author Luca Del Bianco <vshjxyz@gmail.com> | |
# | |
# This script will do the following things | |
# | |
# 1- Call a tastypie api and fetch the JSON results | |
# 2- Connect to a redis server | |
# 3- Open / Connect to 1 redis channel for each object in the response | |
# 4- Listen to new connections with socketio | |
# 5- Associate the socketio connections with the redis when a message is received in the redis channel | |
# | |
local_env = not process.env.PORT? | |
port = process.env.PORT or 8001 | |
tastypie_host = if local_env then 'pizzanuvola.com' else 'beta.pizzanuvola.com' | |
tastypie_port = if local_env then 8000 else 80 | |
req = require './libs/getJSON' | |
express = require 'express' | |
http = require 'http' | |
redis = require 'redis' | |
url = require 'url' | |
app = express() | |
server = http.createServer app | |
io = require('socket.io').listen server, { log: false } | |
redis_url = url.parse(process.env.REDISCLOUD_URL) if process.env.REDISCLOUD_URL? | |
socketio_connections = {} | |
redis_private_channel = '2c17c6393771ee3048ae34d6b380c5fs' | |
# Starting the socket.io / express server | |
server.listen port | |
tastypie_request_options = | |
host : tastypie_host | |
port : tastypie_port | |
path : '/pos/pos/poslist/' | |
method : 'GET' | |
headers: | |
'Content-Type': 'application/json' | |
io.sockets.on 'connection', (socket) -> | |
console.log "<---> New client connected" | |
socket.on 'message', (message) -> | |
console.log "<--- Message from client [ID: #{@id}]: #{message}" | |
try | |
message = JSON.parse message | |
catch e | |
console.error "Error during the JSON parsing, message: #{message}" | |
# Initializing a new array of connections if | |
socketio_connections[message.pos_name] = {} if not socketio_connections[message.pos_name]? | |
socketio_connections[message.pos_name][@id] = @ | |
@set 'pos_name', message.pos_name | |
socket.on 'disconnect', -> | |
console.log "---X Client disconnected [ID: #{@id}]" | |
@get 'pos_name', (err, pos_name) => | |
delete socketio_connections[pos_name][@id] | |
subscribe_to_channel = (connection, channel) -> | |
# We usubscribe / subscribe just to be sure that we'll be subscribed to the channel once | |
connection.unsubscribe channel | |
connection.subscribe channel | |
console.log "<--- Subscribed to the channel #{channel}" | |
unsubscribe_to_channel = (connection, channel) -> | |
# We unsubscribe from the channel 'channel' | |
connection.unsubscribe channel | |
console.log "X--- Unsubscribed from the channel #{channel}" | |
# Calling the tastypie api to get the list of pos | |
req.getJSON tastypie_request_options, (status_code, result) -> | |
console.log "There are #{result.objects.length} registered pos instances" | |
if redis_url? | |
redis_client = redis.createClient redis_url.port, redis_url.hostname, {no_ready_check: true} | |
redis_client.auth redis_url.auth.split(":")[1] | |
else | |
redis_client = redis.createClient null, null, null, {no_ready_check: true} | |
console.log "---> Opening the redis connection ~ #{redis_client.host}:#{redis_client.port}..." | |
# Wait for connection to become established. | |
redis_client.on 'ready', => | |
console.log "<--- Connected to the redis server" | |
redis_client.on 'message', (channel, message) -> | |
try | |
message = JSON.parse message | |
catch e | |
throw "There was an error during the parse of the message: #{message}" | |
if channel is redis_private_channel | |
switch message.action | |
when 'subscribe' | |
subscribe_to_channel redis_client, message.data.pos_name | |
when 'unsubscribe' | |
unsubscribe_to_channel redis_client, message.data.pos_name | |
# Redirects the messages to the correct connected clients | |
console.log "<--- Message received on channel #{channel} ~ action: #{message.action}" | |
if socketio_connections[channel]? | |
for socket_id of socketio_connections[channel] | |
socketio_connections[channel][socket_id].emit channel, message | |
console.log "---> Sent message to the client [ID: #{socket_id}]" | |
for pos in result.objects | |
subscribe_to_channel redis_client, pos.name | |
subscribe_to_channel redis_client, redis_private_channel | |
console.log "Successfully subscribed to #{result.objects.length} channels" | |
redis_client.on 'end', -> | |
console.log "---X Connection to the redis server closed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment