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; });
@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