Sails JavaScript Client SDK
JavaScript SDK for communicating w/ Sails via sockets from Node.js or the browser.
========================================
Contents
Jump to... | |
---|---|
I | Browser |
II | Node.js |
III | Version Notes |
IV | License |
========================================
For the Browser
Installation
The sails.io.js
client comes automatically installed in new Sails projects, but there is nothing project-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.
Using Bower
$ bower install sails.io.js
Always use the version of
sails.io.js
that is compatible with your version of Sails. This repository (and Bower) will always include the version ofsails.io.js
that is compatible with the latest Sails version. If you have an older install, use the copy that is included in theassets/js/dependencies
folder of your Sails app.
Basic Usage
<!-- .... -->
Advanced Usage
Cross-domain
Connect to a server other than the one that served ths project (i.e. on a different domain/subdomain):
Note that in order for
req.session
on a cross-domain server to work, there is a bit of pregaming that sails.io.js does behind the scenes. This is because it relies on cookies, and browsers (thankfully) do not let us access cross-origin cookies. This JavaScript SDK circumvents that issue by (1) detecting a cross-origin scenario by examiningwindow.location
(if available) and comparing it with the connection base URL, then (2) sending a JSONP request to the cross-origin server in order to gain access to a cookie. In order for that to work, the cross-origin sails server must have CORS enabled forhttp://yourdomain.com
so that 3rd-party cookies are granted with the JSONP request. Fortunately, Sails supports this behavior out of the box.For example, imagine the sails.io.js client is being used on a webpage served from a Sails server (or any other kind of server, like nginx) at
http://yourdomain.com
, but you need to connect a WebSocket to a different Sails server athttp://api.your-other-domain.com
. First, sails.io.js will send a JSONP request to the configured "cookie route" (i.e./__getcookie
by default). That particular "cookie route" comes with CORS enabled out of the box, which means it will grant cookies to 3rd party domains. In yourconfig/sockets.js
file, you can restrict cross-domain cookie access to particular domains (i.e.http://yourdomain.com
, in this example)
autoConnect
and/or connect sockets manually
Disable Disable io.socket
and its auto-connecting behavior and/or connect 1 or more sockets manually:
Note that the
io.sails
config functions as the default for all connected sockets, but it can be overridden on a socket-by-socket basis by passing in an object toio.sails.connect(opts)
transports
used to connect to the server
Change the In some cases you may want to change the transorts that the socket client uses to connect to the server, and vice versa. For instance, some server environments--notably Heroku--do not support "sticky load balancing", causing the "polling" transport to fail. In these cases, you should first change the transports listed in the config/sockets.js
file in your Sails app. Then change the transports in the client by setting io.sails.transports
:
<script type="text/javascript" src="./path/to/bower_components/sails.io.js"></script>
<script type="text/javascript">
io.sails.transports = ['websocket'];
</script>
========================================
For Node.js
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.
Installation
$ npm install socket.io-client$ npm install sails.io.js
Basic Usage
var socketIOClient = ;var sailsIOClient = ; // 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 = ; // Set some options:// (you have to specify the host and port of the Sails backend when using this library from Node.js)iosailsurl = 'http://localhost:1337';// ... // Send a GET request to `http://localhost:1337/hello`:iosocket;
See the tests in this repository for more examples.
========================================
Version
This repository holds the socket client SDK for Sails versions 0.10.0 and up. If you're looking for the SDK for the v0.9.x releases of Sails, the source is located here.
========================================
License
MIT © 2014- Mike McNeil, Balderdash & contributors
This module is part of the Sails framework, and is free and open-source under the MIT License.