<commit_before>'use strict';

var redis         = require('redis');
var request       = require('request');
var socketIO      = require('socket.io');
var socketIORedis = require('socket.io-redis');

var io     = socketIO();
var config = require('./config');

/**
 * Do nothing.
 */
var noop = function() { }

/**a
 * Set Redis as our 'MemoryStore' for much scalability.
 *
 * NOTE We need to create custom pub and sub clients because of authentication
 *      set in the cloud.
 */
var pubClient = redis.createClient(
	config.redis.port, config.redis.host, {
		auth_pass: config.redis.opts.auth_pass
	}
);

var subClient = redis.createClient(
	config.redis.port, config.redis.host, {
		auth_pass:      config.redis.opts.auth_pass,
		detect_buffers: true
	}
);

io.adapter(socketIORedis({
	pubClient: pubClient,
	subClient: subClient
}));

/**
 * Authenticate incoming requests.
 */
io.use(function handshake(socket, next) {
	var token = socket.request._query['access-token'];

	if(!token) {
		return next(new Error('Authorization required'));
	}

	var options = {
		'url': config.api + '/auth',
		'headers': {
			'Authorization': 'Bearer ' + token + ''
		}
	}

	request.get(options, function(err, res, body) {
		if(err) {
			return next(err);<commit_msg>Add error handling to pub and sub clients.<commit_after>'use strict';

var redis         = require('redis');
var request       = require('request');
var socketIO      = require('socket.io');
var socketIORedis = require('socket.io-redis');

var io     = socketIO();
var config = require('./config');

/**
 * Do nothing.
 */
var noop = function() { }

/**
 * Set Redis as our 'MemoryStore' for much scalability.
 *
 * NOTE We need to create custom pub and sub clients because of authentication
 *      set in the cloud.
 */
var pubClient = redis.createClient(
	config.redis.port, config.redis.host, {
		auth_pass: config.redis.opts.auth_pass
	}
);

var subClient = redis.createClient(
	config.redis.port, config.redis.host, {
		auth_pass:      config.redis.opts.auth_pass,
		detect_buffers: true
	}
);

var redisAdapter = socketIORedis({
	pubClient: pubClient,
	subClient: subClient
});

redisAdapter.pubClient.on('error', console.log);
redisAdapter.subClient.on('error', console.log);

io.adapter(redisAdapter);

/**
 * Authenticate incoming requests.
 */
io.use(function handshake(socket, next) {
	var token = socket.request._query['access-token'];

	if(!token) {
		return next(new Error('Authorization required'));
	}

	var options = {
		'url': config.api + '/auth',
		'headers': {
			'Authorization': 'Bearer ' + token + ''
		}
	}

	request.get(options, function(err, res, body) {
		if(err) {
			return next(err);
{'fraction_non_alphanumeric': 0.15099288122892468, 'fraction_numerical': 0.0, 'mean_word_length': 3.708994708994709, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6973572', 'n_tokens_mistral': 922, 'n_tokens_neox': 889, 'n_words': 216}