razorframe
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,4 +0,14 @@ | ||
const { EventEmitter } = require('events'); | ||
const { EventEmitter } = require('events'); | ||
const LinkedList = require('./LinkedList'); | ||
const express = require('express'); | ||
const cluster = require('cluster'); | ||
const redis = require('redis'); | ||
// const socket = require('socket.io'); | ||
const redisAdapter = require('socket.io-redis'); | ||
const sticky = require('sticky-session'); | ||
const num_workers = require('os').cpus().length || 1; | ||
const app = express(); | ||
/** | ||
/**Razorframe constructor | ||
* | ||
* MSG parameters | ||
@@ -9,7 +19,5 @@ * @param {string} MSG.contents => the message value | ||
*/ | ||
class Razorframe { | ||
constructor() { | ||
this.storage = {}; | ||
this.length = 0; | ||
this.storage = new LinkedList(); | ||
this.notification = new EventEmitter(); | ||
@@ -19,48 +27,92 @@ } | ||
enqueue(MSG) { | ||
console.log('enqueue has run'); | ||
if (!MSG) console.error('Error: must pass in valid msg object!'); | ||
if (!MSG.contents) console.error('Error: contents should not be null'); | ||
if (!MSG.eventOut) console.error("Error: MSG object must contain valid outbound event name"); | ||
else if (!MSG.contents) console.error('Error: contents should not be null'); | ||
else if (!MSG.eventOut) console.error("Error: MSG object must contain valid outbound event name"); | ||
this.storage[this.length] = MSG; | ||
this.length++; | ||
this.dequeue(); // maybe need to throttle each enq : dq call | ||
else { | ||
this.storage.push(MSG); | ||
this.notification.emit('enq', this.storage.length); | ||
} | ||
} | ||
dequeue() { | ||
if (!this.length) console.error('Error: message queue is currently empty!'); | ||
let MSG = this.storage[0]; | ||
delete this.storage[0]; | ||
for (let i in this.storage) { | ||
this.storage[i - 1] = this.storage[i]; | ||
console.log('dequeue has run.'); | ||
if (this.storage.length === 0) console.error('Error: message queue is currently empty!'); | ||
else { | ||
let MSG = this.storage.pop(); | ||
this.notification.emit('deq', MSG); | ||
} | ||
this.length--; | ||
} | ||
} | ||
this.notification.emit('deq', MSG); | ||
let io; | ||
// const { write } = this.NEURON; | ||
// write(MSG.contents); | ||
// this.broadcastOthers(MSG); | ||
// this.broadcastAll(MSG); | ||
} | ||
const rz = { | ||
razorframe: new Razorframe(), | ||
broadcastOthers(MSG) { | ||
const { socket, contents, eventOut } = MSG; | ||
socket.broadcast.emit(eventOut, contents); | ||
} | ||
init(http, config) { | ||
io = require('socket.io')(http); | ||
broadcastAll(MSG) { | ||
const { io, contents, eventOut } = MSG; | ||
io.emit(eventOut, contents); | ||
if (!sticky.listen(http, config.port)) { | ||
// Master code | ||
http.once('listening', function () { | ||
console.log('server started on 3000 port'); | ||
}); | ||
} else { | ||
// Worker code | ||
handleSockets(config); | ||
} | ||
}, | ||
// If errors occur during database writes, the message will be sent to onError function. If it is the first time the message has caused an error, it will be reentered into the queue for a second attempt at a database write, but it will have an error flag. If the same message comes back, it will emit an error message to the client. | ||
onError(MSG) { | ||
if (MSG.error) { | ||
console.log('inside error function with msg:', MSG.contents); | ||
io.to(MSG.id).emit('error', `There was an error writing "${MSG.contents}" to the database.`); | ||
} else { | ||
MSG.error = true; | ||
this.razorframe.enqueue(MSG); | ||
} | ||
} | ||
serverOn(event, data) { } | ||
enqueueAsyncBatch(MSG) { } | ||
dequeueAsyncBatch() { } | ||
} | ||
module.exports = Razorframe; | ||
const handleSockets = (config) => { | ||
const { write, show } = config; | ||
io.on('connection', (socket) => { | ||
console.log('a user connected! 💃'); | ||
show(socket); | ||
socket.on('disconnect', () => console.log('user disconnected!')); | ||
// Client listeners | ||
socket.on('msgSent', (MSG, id) => { | ||
MSG.id = socket.id; | ||
rz.razorframe.enqueue(MSG); | ||
}); | ||
}); | ||
// Server-side listeners | ||
rz.razorframe.notification.on('enq', (data) => { | ||
rz.razorframe.dequeue(); | ||
}); | ||
rz.razorframe.notification.on('deq', (MSG) => { | ||
write(MSG); | ||
if (!MSG.error) io.emit(MSG.eventOut, MSG.contents); | ||
// Potential way to broadcast error message to client who is source of error | ||
// io.to(MSG.id).emit(MSG.eventOut, MSG.contents); | ||
}); | ||
}; | ||
module.exports = { Razorframe, rz }; | ||
/** | ||
@@ -67,0 +119,0 @@ * Messaging queue module for razorframe. |
{ | ||
"name": "razorframe", | ||
"version": "1.0.2", | ||
"description": "A real-time database streaming library.", | ||
"main": "./lib/Razorbrain.js", | ||
"version": "1.0.3", | ||
"description": "Empowering real-time databases in Node.js", | ||
"main": "./lib/Razorframe.js", | ||
"scripts": { | ||
@@ -27,4 +27,9 @@ "test": "echo \"Error: no test specified\" && exit 1" | ||
"dependencies": { | ||
"socket.io": "^1.6.0" | ||
"express": "^4.14.0", | ||
"parse-redis-url": "0.0.2", | ||
"redis": "^2.6.3", | ||
"socket.io": "^1.6.0", | ||
"socket.io-redis": "^2.0.1", | ||
"sticky-session": "^1.1.2" | ||
} | ||
} |
@@ -1,3 +0,5 @@ | ||
# razorframe<sup>(beta)</sup> | ||
####*Empowering real-time databases in Node.js* | ||
# razorframe<sup>(beta)</sup> [![npm version](https://badge.fury.io/js/razorframe.svg)](https://badge.fury.io/js/razorframe) | ||
####*Empowering real-time databases in Node.js* | ||
____________________________________________________________________ | ||
@@ -26,3 +28,3 @@ | ||
/** | ||
* NEURON parameters - passes into rb any user-defined callbacks | ||
* config parameters - passes into rb any user-defined callbacks | ||
* @param - {Object} http => instantiate an http server | ||
@@ -32,3 +34,3 @@ * @param - {Function} write => a DB write callback (user-defined) | ||
*/ | ||
const NEURON = { | ||
const config = { | ||
write: addToDb, | ||
@@ -40,6 +42,6 @@ show: showAll, | ||
* Instantiate razorframe passing in Node's http object | ||
* (to connect with your server) as well as the Neuron object | ||
* (to connect with your server) as well as the config object | ||
* which contains all the user-defined callbacks | ||
*/ | ||
rz(http, NEURON); | ||
rz(http, config); | ||
``` | ||
@@ -89,7 +91,7 @@ | ||
###Support | ||
Tested in Chrome 55 & Node 6. | ||
Tested in Chrome 55 & Node 6/7. | ||
GitHub Issues: <https://github.com/team-emt/razorframe/issues> | ||
###Contributions | ||
❤️Contributions welcome! | ||
❤️ Contributions welcome! | ||
Please see out GitHub repo at: <https://github.com/team-emt/razorframe> | ||
@@ -96,0 +98,0 @@ |
8874
131
97
6
+ Addedexpress@^4.14.0
+ Addedparse-redis-url@0.0.2
+ Addedredis@^2.6.3
+ Addedsocket.io-redis@^2.0.1
+ Addedsticky-session@^1.1.2
+ Addedaccepts@1.3.8(transitive)
+ Addedarray-flatten@1.1.1(transitive)
+ Addedasync@2.1.4(transitive)
+ Addedbase64-js@0.0.2(transitive)
+ Addedbody-parser@1.20.3(transitive)
+ Addedbops@0.0.7(transitive)
+ Addedbytes@3.1.2(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcontent-disposition@0.5.4(transitive)
+ Addedcontent-type@1.0.5(transitive)
+ Addedcookie@0.7.1(transitive)
+ Addedcookie-signature@1.0.6(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddepd@2.0.0(transitive)
+ Addeddestroy@1.2.0(transitive)
+ Addeddouble-ended-queue@2.1.0-0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedee-first@1.1.1(transitive)
+ Addedencodeurl@1.0.22.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedescape-html@1.0.3(transitive)
+ Addedetag@1.8.1(transitive)
+ Addedexpress@4.21.2(transitive)
+ Addedfinalhandler@1.3.1(transitive)
+ Addedforwarded@0.2.0(transitive)
+ Addedfresh@0.5.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedip@1.1.9(transitive)
+ Addedipaddr.js@1.9.1(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmerge-descriptors@1.0.3(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.6.0(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addedmsgpack-js@0.3.0(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedon-finished@2.4.1(transitive)
+ Addedparse-redis-url@0.0.2(transitive)
+ Addedparseurl@1.3.3(transitive)
+ Addedpath-to-regexp@0.1.12(transitive)
+ Addedproxy-addr@2.0.7(transitive)
+ Addedqs@6.13.0(transitive)
+ Addedrange-parser@1.2.1(transitive)
+ Addedraw-body@2.5.2(transitive)
+ Addedredis@2.6.32.8.0(transitive)
+ Addedredis-commands@1.7.0(transitive)
+ Addedredis-parser@2.6.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsend@0.19.0(transitive)
+ Addedserve-static@1.16.2(transitive)
+ Addedsetprototypeof@1.2.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedsocket.io-redis@2.0.1(transitive)
+ Addedstatuses@2.0.1(transitive)
+ Addedsticky-session@1.1.2(transitive)
+ Addedto-utf8@0.0.1(transitive)
+ Addedtoidentifier@1.0.1(transitive)
+ Addedtype-is@1.6.18(transitive)
+ Addeduid2@0.0.3(transitive)
+ Addedunpipe@1.0.0(transitive)
+ Addedutils-merge@1.0.1(transitive)
+ Addedvary@1.1.2(transitive)