Skip to content

@Blackmist /config.json
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
example of logging to table storage on socket.io messages. Reads storage account name and key, along with partition key and table name from either the config.json file, or environment variables STORAGE_NAME, STORAGE_KEY, PARTITION_KEY, TABLE_NAME. based on the example chat application from the Socket.io repository.
{
"STORAGE_NAME": "storage name",
"STORAGE_KEY": "storage key",
"PARTITION_KEY": "partition key",
"TABLE_NAME": "table name"
}
/**
* Module dependencies.
*/
var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, sio = require('socket.io')
, uuid = require('node-uuid')
, nconf = require('nconf')
, azure = require('azure');
nconf.env()
.file({ file: 'config.json' });
var tableName = nconf.get("TABLE_NAME")
, partitionKey = nconf.get("PARTITION_KEY")
, accountName = nconf.get("STORAGE_NAME")
, accountKey = nconf.get("STORAGE_KEY");
/**
* App.
*/
var app = express.createServer();
var storageClient = azure.createTableService(accountName, accountKey);
storageClient.createTableIfNotExists(tableName, function(error){
if(!error){
console.log("created or found table");
}
});
/**
* App configuration.
*/
app.configure(function () {
app.use(stylus.middleware({ src: __dirname + '/public', compile: compile }));
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname);
app.set('view engine', 'jade');
function compile (str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
};
});
/**
* App routes.
*/
app.get('/', function (req, res) {
res.render('index', { layout: false });
});
/**
* App listen.
*/
app.listen(process.env.port||1337, function () {
var addr = app.address();
});
/**
* Socket.IO server (single process only)
*/
var io = sio.listen(app)
, nicknames = {};
io.configure('production', function(){
io.set('transports',['xhr-polling']);
});
io.sockets.on('connection', function (socket) {
socket.on('user message', function (msg) {
var chatRecord1={
PartitionKey: partitionKey,
RowKey: uuid(),
Username: socket.nickname
};
insertEntity(chatRecord1);
socket.broadcast.emit('user message', socket.nickname, msg);
});
socket.on('nickname', function (nick, fn) {
if (nicknames[nick]) {
fn(true);
} else {
fn(false);
nicknames[nick] = socket.nickname = nick;
socket.broadcast.emit('announcement', nick + ' connected');
io.sockets.emit('nicknames', nicknames);
}
});
socket.on('disconnect', function () {
if (!socket.nickname) return;
delete nicknames[socket.nickname];
socket.broadcast.emit('announcement', socket.nickname + ' disconnected');
socket.broadcast.emit('nicknames', nicknames);
});
});
function insertEntity(record){
storageClient.insertEntity(tableName, record, function(error){
if(!error){
console.log("entity inserted");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.