Skip to content

Instantly share code, notes, and snippets.

@guerrerocarlos
Created September 6, 2012 05:07
Show Gist options
  • Select an option

  • Save guerrerocarlos/3651490 to your computer and use it in GitHub Desktop.

Select an option

Save guerrerocarlos/3651490 to your computer and use it in GitHub Desktop.
loading socket.io using require.js
// Require.js allows us to configure shortcut alias
require.config({
// The shim config allows us to configure dependencies for
// scripts that do not call define() to register a module
shim: {
'socketio': {
exports: 'io'
},
'underscore': {
exports: '_'
},
'backbone': {
deps: [
'underscore',
'jquery'
],
exports: 'Backbone'
}
},
paths: {
jquery: 'jquery.min',
underscore: 'lodash.min',
backbone: 'backbone',
socketio: '../socket.io/socket.io',
}
});
define([
'jquery',
'backbone',
'socketio',
], function( $, Backbone, io ) {
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
//Ready to write Backbone Models and Socket.io communication protocol in here :)
});
@nullivex

nullivex commented Nov 5, 2013

Copy link
Copy Markdown

Thanks this helped me get that working.

@CurtisSV

Copy link
Copy Markdown

This saved me a lot of frustration! Thanks!

@felixhammerl

Copy link
Copy Markdown

my lazyness thanks you :)

@korczis

korczis commented Jan 23, 2014

Copy link
Copy Markdown

great!

@srlm-io

srlm-io commented Apr 15, 2014

Copy link
Copy Markdown

The most recent version of socket.io (0.9.16) is AMD compatible, so you don't need to do much at all. For my application the socket.io server is on a different domain, so we can include the path argument to pull the script from the correct location, but if it's on the same domain you could just require \socket.io\socket.io.

    // socket.io serves up the script ready to go. socket.io.js has the following lines:
    // if (typeof define === "function" && define.amd) {
    //   define([], function () { return io; });
    // }

    // All we need to do is tell it the path to our server:
    require.config({
        paths: {
            socketio: 'http://my.cross.domain.server.com/socket.io/socket.io'
        }
    });

    require(['socketio'], function(io) {
        var socket = io.connect('my.cross.domain.server.com');
        console.log('socket connected');
    });

@hongshan5

Copy link
Copy Markdown

thanks very much :)

@EderRoger

Copy link
Copy Markdown

Thanks !! Solved my issue!

@flyfly6

flyfly6 commented Aug 7, 2014

Copy link
Copy Markdown

great! Thanks very much!

@chul-hyun

Copy link
Copy Markdown

Thanks!! ><

@lwiechec

Copy link
Copy Markdown

yup, thanks!

@herlon214

Copy link
Copy Markdown

thanks 😄

@web-jenezis

Copy link
Copy Markdown

thanks a lot for this tip!!!

@ryang420

Copy link
Copy Markdown

Thanks! Fixed my issue.

@arbaouimehdi

Copy link
Copy Markdown

Thank you.

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