Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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

This comment has been minimized.

Show comment Hide comment
@nullivex

nullivex Nov 5, 2013

Thanks this helped me get that working.

nullivex commented Nov 5, 2013

Thanks this helped me get that working.

@CurtisSV

This comment has been minimized.

Show comment Hide comment
@CurtisSV

CurtisSV Dec 23, 2013

This saved me a lot of frustration! Thanks!

This saved me a lot of frustration! Thanks!

@felixhammerl

This comment has been minimized.

Show comment Hide comment
@felixhammerl

felixhammerl Jan 13, 2014

my lazyness thanks you :)

my lazyness thanks you :)

@korczis

This comment has been minimized.

Show comment Hide comment
@korczis

korczis Jan 23, 2014

great!

korczis commented Jan 23, 2014

great!

@srlm-io

This comment has been minimized.

Show comment Hide comment
@srlm-io

srlm-io Apr 15, 2014

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');
    });

srlm-io commented Apr 15, 2014

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

This comment has been minimized.

Show comment Hide comment
@hongshan5

hongshan5 May 5, 2014

thanks very much :)

thanks very much :)

@EderRoger

This comment has been minimized.

Show comment Hide comment
@EderRoger

EderRoger Jul 2, 2014

Thanks !! Solved my issue!

Thanks !! Solved my issue!

@flyfly6

This comment has been minimized.

Show comment Hide comment
@flyfly6

flyfly6 Aug 7, 2014

great! Thanks very much!

flyfly6 commented Aug 7, 2014

great! Thanks very much!

@vomvoru

This comment has been minimized.

Show comment Hide comment
@vomvoru

vomvoru Aug 19, 2014

Thanks!! ><

vomvoru commented Aug 19, 2014

Thanks!! ><

@lwiechec

This comment has been minimized.

Show comment Hide comment
@lwiechec

lwiechec Aug 29, 2014

yup, thanks!

yup, thanks!

@herlon214

This comment has been minimized.

Show comment Hide comment
@herlon214

herlon214 Apr 28, 2015

thanks 😄

thanks 😄

@web-jenezis

This comment has been minimized.

Show comment Hide comment
@web-jenezis

web-jenezis Nov 29, 2015

thanks a lot for this tip!!!

thanks a lot for this tip!!!

@ryang420

This comment has been minimized.

Show comment Hide comment
@ryang420

ryang420 Jan 21, 2016

Thanks! Fixed my issue.

Thanks! Fixed my issue.

@freemh

This comment has been minimized.

Show comment Hide comment
@freemh

freemh Jan 29, 2016

Thank you.

freemh commented Jan 29, 2016

Thank you.

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