Javascript SDK for communicating w/ a Sails server via WebSockets/socket.io.
npm install sails.io.js
Sails JavaScript Client SDK
JavaScript SDK for communicating w/ Sails via sockets from Node.js or the browser.
> Why would I use this from a Node script?
>
> Most commonly, this SDK is useful on the backend when writing tests. However, any time you'd want to use a WebSocket or Socket.io client from Node to talk to a Sails server, you can use this module to allow for the ordinary usage you're familiar with in the browser-- namely using the socket interpreter to simulate HTTP over WebSockets.
``sh`
$ npm install socket.io-client --save
$ npm install sails.io.js --save
`js
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
// Instantiate the socket client (io)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
// ...
// Send a GET request to http://localhost:1337/hello:
io.socket.get('/hello', function serverResponded (body, JWR) {
// body === JWR.body
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// ...
// more stuff
// ...
// When you are finished with io.socket, or any other sockets you connect manually,
// you should make sure and disconnect them, e.g.:
io.socket.disconnect();
// (note that there is no callback argument to the .disconnect method)
});
`
See the tests in this repository for more examples.
========================================
The sails.io.js client comes automatically installed in new Sails projects, but there is nothing _app-specific_ about the client SDK. You can just as easily copy and paste it yourself, get it from Bower, or just link a script tag directly to a hosted CDN.
> Always use the version of sails.io.js that is compatible with your version of Sails. The master branch of this repository includes the bleeding edge version of sails.io.js that is compatible with the master branch of Sails core and of sails-hook-sockets. If you have an older install, use the copy of sails.io.js that is included in the assets/ folder of your Sails app.
`html
// io is available as a global.io.socket
// will connect automatically, but at this point in the DOM, it is not ready yetio
// (think of $(document).ready() from jQuery)
//
// Fortunately, this library provides an abstraction to avoid this issue.
// Requests you make before is ready will be queued and replayed automatically when the socket connects.io.sails
// To disable this behavior or configure other things, you can set properties on .io.sails.autoConnect
// You have one cycle of the event loop to set to false before the auto-connection
// behavior starts.
io.socket.get('/hello', function serverResponded (body, JWR) {
// JWR ==> "JSON WebSocket Response"
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
// first argument body === JWR.bodyJQuery.get()
// (just for convenience, and to maintain familiar usage, a la )
});