Gathering detailed insights and metrics for sails.io.js-dist
Gathering detailed insights and metrics for sails.io.js-dist
Gathering detailed insights and metrics for sails.io.js-dist
Gathering detailed insights and metrics for sails.io.js-dist
Browser SDK for communicating w/ Sails via sockets
npm install sails.io.js-dist
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
183 Stars
383 Commits
118 Forks
15 Watching
8 Branches
25 Contributors
Updated on 10 Sept 2024
Minified
Minified + Gzipped
JavaScript (98.85%)
HTML (1.15%)
Cumulative downloads
Total Downloads
Last day
-0.7%
4,022
Compared to previous day
Last week
-10.6%
22,150
Compared to previous week
Last month
-10.4%
102,135
Compared to previous month
Last year
-18%
1,504,798
Compared to previous year
No dependencies detected.
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.
1$ npm install socket.io-client --save 2$ npm install sails.io.js --save
1var socketIOClient = require('socket.io-client'); 2var sailsIOClient = require('sails.io.js'); 3 4// Instantiate the socket client (`io`) 5// (for now, you must explicitly pass in the socket.io client when using this library from Node.js) 6var io = sailsIOClient(socketIOClient); 7 8// Set some options: 9// (you have to specify the host and port of the Sails backend when using this library from Node.js) 10io.sails.url = 'http://localhost:1337'; 11// ... 12 13// Send a GET request to `http://localhost:1337/hello`: 14io.socket.get('/hello', function serverResponded (body, JWR) { 15 // body === JWR.body 16 console.log('Sails responded with: ', body); 17 console.log('with headers: ', JWR.headers); 18 console.log('and with status code: ', JWR.statusCode); 19 20 // ... 21 // more stuff 22 // ... 23 24 25 // When you are finished with `io.socket`, or any other sockets you connect manually, 26 // you should make sure and disconnect them, e.g.: 27 io.socket.disconnect(); 28 29 // (note that there is no callback argument to the `.disconnect` method) 30}); 31
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 ofsails.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 theassets/
folder of your Sails app.
1 </body> 2 3 <!-- Import SDK (if using the linker, then this will be injected automatically) --> 4 <script type="text/javascript" src="/dependencies/sails.io.js"></script> 5 6 <!-- Example usage --> 7 <script type="text/javascript"> 8 9 // `io` is available as a global. 10 // `io.socket` will connect automatically, but at this point in the DOM, it is not ready yet 11 // (think of $(document).ready() from jQuery) 12 // 13 // Fortunately, this library provides an abstraction to avoid this issue. 14 // Requests you make before `io` is ready will be queued and replayed automatically when the socket connects. 15 // To disable this behavior or configure other things, you can set properties on `io.sails`. 16 // You have one cycle of the event loop to set `io.sails.autoConnect` to false before the auto-connection 17 // behavior starts. 18 19 io.socket.get('/hello', function serverResponded (body, JWR) { 20 21 // JWR ==> "JSON WebSocket Response" 22 console.log('Sails responded with: ', body); 23 console.log('with headers: ', JWR.headers); 24 console.log('and with status code: ', JWR.statusCode); 25 26 // first argument `body` === `JWR.body` 27 // (just for convenience, and to maintain familiar usage, a la `JQuery.get()`) 28 }); 29 </script> 30</html>
========================================
The io.sails
config functions as the default for all connected sockets, allowing you to change client behavior globally. It can be overridden on a socket-by-socket basis by passing in an object to io.sails.connect(opts)
Connect to a server other than the one that served ths project (i.e. on a different domain/subdomain):
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3io.sails.url = 'https://api.mysite.com'; 4</script>
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 manuallyDisable io.socket
and its auto-connecting behavior and/or connect 1 or more sockets manually:
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3io.sails.autoConnect = false; 4 5// e.g. at some point later, connect 3 sockets, using default settings 6setTimeout(function (){ 7 8 // socket0 and socket1 will use default settings from `io.sails` 9 var socket0 = io.sails.connect(); 10 var socket1 = io.sails.connect(); 11 12 // but socket2's `url` option will be overridden as specified: 13 var socket2 = io.sails.connect('https://api.mysite.com'); 14}, 1000); 15</script>
initialConnectionHeaders
Disable session support for all connecting sockets
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3io.sails.initialConnectionHeaders = {nosession: true}; 4</script>
Disable session support on a per-socket basis
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3io.sails.autoConnect = false; 4// socket 1 will have session disabled 5var socket1 = io.sails.connect('http://localhost', {initialConnectionHeaders: {nosession: true}}); 6// socket 2 will have session enabled 7var socket2 = io.sails.connect('http://localhost'); 8</script>
Set a x-csrf-token
header to be sent with every request made using io.socket.*
methods:
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3io.sails.headers = { 4 "x-csrf-token": someToken, 5}; 6// This POST request will now include the x-csrf-token header 7io.socket.post("/foo", someData, someCallback); 8</script>
transports
used to connect to the serverIn 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
:
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3 io.sails.transports = ['websocket']; 4</script>
rejectUnauthorized
setting used to connect to the serverAs of socket.io-client version 1.4.6 and engine.io-client 1.6.9, if you are
using SSL certificates to connect, rejectUnauthorized
defaults to true
if not explicitly set. To keep the old behavior (useful for development and
testing/continuous integration environments), set it to false on the io.sails
object:
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3 io.sails.rejectUnauthorized = false; 4</script>
To use this in an AMD environment, use the sails.io.js in the root of this repo, not in dist. The dist build bundles a version of the socket.io client which will cause errors when trying to load two anonymous AMD modules from the same file. The file in root is not bundled with socket.io
Usage with AMD will be very similar as node. Require in sails.io, socket.io, and instantiate the sails.io client:
1define(['path/to/socketIOClient', 'path/to/sailsIOClient'], function(socketIOClient, sailsIOClient) { 2 var io = sailsIOClient(socketIOClient); 3 io.sails.url = 'http:/example.com'; 4 5 io.socket.get('/example/path', { data: 'example' }, function(response) { 6 console.log('got response', response) 7 }); 8});
Sails.io.js console.log messages are automatically muted in production environments. You can set the environment manually via io.sails.environment
:
1<script type="text/javascript" src="/dependencies/sails.io.js"></script> 2<script type="text/javascript"> 3 io.sails.environment = 'production'; 4</script>
If not specified manually, sails.io.js will assume the development
environment unless it is loaded from a URL that ends in *.min.js
or #production
, e.g. production.min.js
or scripts.js#production
.
========================================
If you have further questions or are having trouble, click here.
To report a bug, click here.
Please observe the guidelines and conventions laid out in the Sails project contribution guide when opening issues or submitting pull requests.
This repository holds the socket client SDK for Sails versions 0.11.0 and up. If you're looking for the SDK for the v0.9.x releases of Sails, the source is located here. If you're looking for v0.10.x, check out the relevant tags.
This package is part of the Sails framework, and is free and open-source under the MIT License.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 1/24 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More