Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created February 10, 2014 06:02
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mikermcneil/8911041 to your computer and use it in GitHub Desktop.
Save mikermcneil/8911041 to your computer and use it in GitHub Desktop.
// To disable socket.io, disable the sockets hook (you'll have to disable the pubsub hook as well)
// This is a replacement for the default app.js file:
require('sails').lift({
hooks: {
sockets: false,
pubsub: false
}
}, function doneLifting (err) { if (err) throw err; });
@jybleau
Copy link

jybleau commented May 21, 2014

Hi. How do I apply it to the latest app.js code ?
Version 0.9.16 is:

// Start sails and pass it command line arguments
require('sails').lift(require('optimist').argv);

Do I simply replace require('optimist').argv ?

@zwrose
Copy link

zwrose commented Jun 15, 2014

I have the same question as jybleau - how does this apply to the 0.9.16 app.js as posted above?

Thanks!

@russellsantos
Copy link

This should work for both of you

var config = sails.util.merge(require('optimist').argv,{
  hooks: {
    sockets: false,
    pubsub: false 
  }
});
require('sails').lift(config);

@HaNdTriX
Copy link

HaNdTriX commented Jul 9, 2014

FYI in v0.10 you can use the .sailsrc for that kind of job:

{
    "hooks": {
        "sockets": false,
        "pubsub": false 
    }
}

@imorti
Copy link

imorti commented Jul 25, 2014

ok so I noticed something. I used the line above from @mikermcneil to disable sockets on the server. However, when I comment out the includes in layout.ejs:
<script src="/linker/js/socket.io.js"></script>
<script src="/linker/js/sails.io.js"></script>

After sails lift they're fine. The second I reload the page, my commenting out is replaced, they are back to being included, and sockets starts up again. VERY strange. This is with sailsjs -v 0.9.16

@micahlisonbee
Copy link

imorti, you need to delete the sails.io.js file or it get's auto-generated as a script link into your layout file.

@macedd
Copy link

macedd commented Dec 2, 2014

What if want to disable only for certain endpoints ?

@mikermcneil
Copy link
Author

@ThiagoF I usually write up a policy along these lines and apply it to the relevant actions or routes:

function noSockets(req, res, onwards) {
  if (req.isSocket) {
    return res.badRequest('Sorry, this endpoint is not accessible via sockets.');
  }
  return onwards();
}

@rishabhmhjn
Copy link

@mikermcneil disabling the sockets hook doesn't seem to be working!

.sailsrc
{
  "hooks": {
    "blueprints": false,
    "http": false,
    "sockets": false,
    "policies": false,
    "views": false,
    "pubsub": false,
    "request": false,
    "responses": false,
    "controllers": false,
    "csrf": false,
    "cors": false,
    "session": false
  },
  "generators": {
    "modules": {}
  },
  "log": {
    "noShip": true
  }
}
my log
error: A hook (`sockets`) failed to load!
{ [Error (SAILS:HOOK:SOCKETS:DEPENDS_ON_HOOK):: Cannot use `sockets` hook without the `http` hook.]
  code: 'SAILS:HOOK:SOCKETS:DEPENDS_ON_HOOK',
  name: 'Error (SAILS:HOOK:SOCKETS:DEPENDS_ON_HOOK):',
  status: 500,
  message: 'Cannot use `sockets` hook without the `http` hook.',
  stack: 'Error (SAILS:HOOK:SOCKETS:DEPENDS_ON_HOOK):: Cannot use `sockets` hook without the `http` hook.
      at new constructor (/path/to/app/node_modules/sails-hook-sockets/standalone/create-error-constructor.js:38:16)
      at Errorpack.factory [as DEPENDS_ON_HOOK] (/path/to/app/node_modules/sails-hook-sockets/standalone/create-error-factory.js:34:12)
      at waitForOtherHooks (/path/to/app/node_modules/sails-hook-sockets/lib/initialize.js:41:31)
      at Hook.initialize (/path/to/app/node_modules/sails-hook-sockets/lib/initialize.js:55:7)
      at Hook.bound [as initialize] (/path/to/app/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
      at /path/to/app/node_modules/sails/lib/hooks/index.js:74:14
      at /path/to/app/node_modules/sails/node_modules/async/lib/async.js:451:17
      at /path/to/app/node_modules/sails/node_modules/async/lib/async.js:441:17
      at _each (/path/to/app/node_modules/sails/node_modules/async/lib/async.js:46:13)
      at Immediate.taskComplete (/path/to/app/node_modules/sails/node_modules/async/lib/async.js:440:13)
      at processImmediate [as _immediateCallback] (timers.js:384:17)' }

@alexisbrtl
Copy link

Same problem as rishabhmhjn, I tried setting in .sailsrc file:

"hook":{
    "pubsub":false,
    "sockets":false
}

But the sails.sockets and sails.io objects are still working.

@jacqueslareau
Copy link

For others coming here, on windows, npm installs sails-hook-socket in the top director now. So it's disabled in sails config, but reloaded again when checking for user hooks to load. See balderdashy/sails#3317

@ultrasaurus
Copy link

FYI: you need to also remove client-side javascript for sockets, see: balderdashy/sails#3580 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment