Skip to content

Instantly share code, notes, and snippets.

@wearhere
Last active August 10, 2016 22:32
Show Gist options
  • Save wearhere/b4b3671c56ec799cdf089d5fa6587542 to your computer and use it in GitHub Desktop.
Save wearhere/b4b3671c56ec799cdf089d5fa6587542 to your computer and use it in GitHub Desktop.
Pseudocode for achieving sticky websockets on AWS using per-instance EIPs vended over HTTP.
function connect() {
// Since we'd have sticky HTTP sessions, this will always go to the same instance,
// thus the websocket will connect to the same instance, assuming it's still living.
$.get('/websocket')
.then(function(ipAddress) {
Websocket.connect(ipAddress);
});
}
Websocket.on('disconnection', connect);
// Not sure if this could be an environment variable or would have to be fetched
// like instance metadata.
var MY_EIP;
app.get('/websocket', function(req, res) {
res.send(MY_EIP);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment