Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active July 8, 2018 18:51
Show Gist options
  • Save mikermcneil/8465536 to your computer and use it in GitHub Desktop.
Save mikermcneil/8465536 to your computer and use it in GitHub Desktop.
Client-side (browser) example of a socket.io client for Sails.js <http://sailsjs.org>
// example.js
// ===============================
//
// Client-side (browser) example of how you might connect a socket.io client
// to your Sails backend.
// For the latest docs on talking to Sails via socket.io, check out the new reference section in the Sails docs:
// https://github.com/balderdashy/sails-docs/blob/master/reference/SocketClient.md
// As for some great tutorials, my best recommendation would be to check out @irlnathan's sailscasts:
// http://irlnathan.github.io/sailscasts
// MIT License
// c. 2013, Mike McNeil <@mikermcneil>
// <sailsjs.org>
// NOTE
//
// This example expects and/or creates the following global variables:
//
// (1) `io`
//
// For this example to work, the socket.io client (`socket.io.js`) must
// be included on your page BEFORE this file. This is because we need
// access to the global `io` variable.
//
// To see an example, just check out this app. `sails new` bundles a copy
// of the socket.io client in `assets/socket.io.js`, and the default Gruntfile
// includes it in the proper order before this file.
//
//
// (2) `socket`
//
// In this example, after we finish connecting to the server, we expose the
// connected `socket` as a global-- mainly because this makes it easy to access
// from the browser console.
//
// Check it out!
//
// > socket.get('/parrot', console.log.bind(console));
// > socket.get('/parrot/3', console.log.bind(console));
// > socket.post('/parrot', { name: 'Polly', age: 49 }, console.log.bind(console) );
// > socket.put('/parrot/4', { age: 50 }, console.log.bind(console) );
// > socket.delete('/parrot/3', console.log.bind(console) );
//
//
// BTW in case you were wondering: `console.log.bind(console)`
// is just a Chrome-friendly shortcut for: `function (response) {console.log(response); }`
// Immediately start connecting
socket = io.connect();
typeof console !== 'undefined' &&
console.log('Connecting Socket.io to Sails.js...');
// Attach a listener which fires when a connection is established:
socket.on('connect', function socketConnected() {
typeof console !== 'undefined' &&
console.log(
'Socket is now connected and globally accessible as `socket`.\n' +
'e.g. to send a GET request to Sails via Socket.io, try: \n' +
'`socket.get("/foo", function (response) { console.log(response); })`'
);
// Attach a listener which fires every time the server publishes a message:
socket.on('message', function newMessageFromSails ( message ) {
typeof console !== 'undefined' &&
console.log('New message received from Sails ::\n', message);
});
});
@Jass91
Copy link

Jass91 commented May 8, 2018

why did i get 'io not defined'?

@RobertLbebber
Copy link

Yea there has to be something missing to this because.. well you only have one file and its not even sail.io.js

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