Skip to content

Commit

Permalink
Merge fef5e50 into a402180
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Oct 21, 2016
2 parents a402180 + fef5e50 commit 7b4d80c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ After installing the server, you can run ```redis-cli``` to issue commands to re
- Run ```git push heroku <your branch>:master``` which will push to Heroku and start up a dyno.
- Run ```heroku open``` and view the app running in Heroku
- Run ```heroku run bash``` then run ```mocha``` to execute the test suite

- If you are running the app in more than one dyno, you will need to force the client to communicate with the server only using websockets. To do so, set the config variable SOCKETIO_TRANSPORT_PROTOCOL to websocket or run ```heroku config:set SOCKETIO_TRANSPORT_PROTOCOL=websocket```
If you are running on Heroku and you want to use Google Analytics, store your tracking id in a Heroku config variable called `GOOGLE_ANALYTICS_ID`.

## Using Trace (by RisingStack) for application performance monitoring
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function start() { // eslint-disable-line max-statements

const app = express();
const httpServer = require('http').Server(app);

const io = require('socket.io')(httpServer);
const socketIOSetup = require('./realtime/setupSocketIO');
socketIOSetup.setupNamespace(io);
Expand Down
1 change: 1 addition & 0 deletions view/loadView.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ module.exports = function loadView(app, passport) {
trackingId: viewConfig.trackingId,
user: req.user,
eventThrottle: viewConfig.realtimeEventThrottleMilliseconds,
transportProtocol: viewConfig.socketIOtransportProtocol,
};

const templateVars = Object.assign(
Expand Down
30 changes: 28 additions & 2 deletions view/perspective/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const ONE = 1;

const DEBUG_REALTIME = window.location.href.split(/[&\?]/)
.includes('debug=REALTIME');

const WEBSOCKET_ONLY = window.location.href.split(/[&\?]/)
.includes('protocol=websocket');
const REQ_HEADERS = {
Authorization: u.getCookie('Authorization'),
'X-Requested-With': 'XMLHttpRequest',
Expand Down Expand Up @@ -95,11 +96,36 @@ function handleEvent(eventData, eventTypeName) {
function setupSocketIOClient(persBody) {
const namespace = u.getNamespaceString(persBody);

/*
* if the transprotocol is set, initialize the socketio client with
* the transport protocol options. The transProtocol variable is set in
* perspective.pug
*/
const options = {};
const clientProtocol = transProtocol;
if (clientProtocol) {
/*
* options is used here to set the transport type. For example to only use
* websockets as the transport protocol the options object will be
* { transports: ['websocket'] }. The regex is used to trim the white spaces
* and since clientProtocol is a string of comma seperated values,
* the split function is used to split them out by comma and convert
* it to an array.
*/
options.transports = clientProtocol.replace(/\s*,\s*/g, ',').split(',');
}

/*
* Note: The "io" variable is defined by the "/socket.io.js" script included
* in perspective.pug.
*/
const socket = io(namespace);
let socket;
if (WEBSOCKET_ONLY) {
socket = io(namespace, { transports: ['websocket'] });
} else {
socket = io(namespace, options);
}

socket.on(eventsQueue.eventType.INTRNL_SUBJ_ADD, (data) => {
handleEvent(data, eventsQueue.eventType.INTRNL_SUBJ_ADD);
});
Expand Down
3 changes: 3 additions & 0 deletions view/perspective/perspective.pug
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ html(lang = 'en')
script(src='/static/socket.io.js')
script.
var realtimeEventThrottleMilliseconds = #{eventThrottle};
script.
var transProtocol = '#{transportProtocol}';

script
if queryParams
| var queryParams = !{queryParams}
Expand Down
4 changes: 4 additions & 0 deletions viewConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ const DEFAULT_THROTTLE_MILLISECS = 4000;
const realtimeEventThrottleMilliseconds =
pe.realtimeEventThrottleMilliseconds || DEFAULT_THROTTLE_MILLISECS;

const socketIOtransportProtocol = pe.SOCKETIO_TRANSPORT_PROTOCOL || null;
module.exports = {
// Make the Google Analytics trackingId available in /view.
trackingId: pe.GOOGLE_ANALYTICS_ID || 'N/A',

// Make the throttle time available in /view.
realtimeEventThrottleMilliseconds,

// Expose the socketIOtransportProtocol variable in the /view
socketIOtransportProtocol,
};

0 comments on commit 7b4d80c

Please sign in to comment.