Search code examples
javascriptnode.jssocket.iorequirejsace-editor

Socket.io and requirejs dependency issue


I have this legacy express project with two servers. I have this two client files:

requirejs.config({
baseUrl: '/js'
, paths: {
    "jquery": "lib/jquery/jquery-2.1.1.min",
    "socket.io" : "lib/socket/socket.io"
  }
});

requirejs(['jquery', 'socket.io'],
function   ($, io) {

  console.log(io);
  var socket = io('http://localhost:3000');

  [....]

And this:

requirejs.config({
  baseUrl: '/js/lib'
  , paths: {
    "ace": "ace/lib/ace"
    , "bcsocket": "/channel/bcsocket"
    , "shareJS": "sharejs/share"
    , "sharejs_ace": "sharejs/ace"
    , "ace_java": "ace/mode/java"
    , "jquery": "jquery/jquery-2.1.1.min"
    , "socket.io": "socket/socket.io"
 }
 , shim: {
   "bcsocket": {
      exports: "BCSocket"
    }
    , "shareJS": {
     exports: "sharejs"
     , deps: ["bcsocket"]
  }
  , "sharejs_ace": {
     deps: ["ace/ace", "shareJS"]
  }
  , "ace_java": {
     deps: ['ace/ace']
  }
}
});

requirejs(['ace/ace', 'shareJS', 'bcsocket', 'sharejs_ace','jquery', 'socket.io'],
   function(ace, sharejs) {
      var editor = ace.edit('editor');
      editor.setTheme('ace/theme/twilight');
      editor.getSession().setMode('ace/mode/java');

      var socket = io('http://localhost:3000');

      console.log(socket)

      socket.emit('hi');

      [....]

The thing is: I can't access the variable io from the second file, only on the first. And even if I manage to join all the dependencies and files in just one, the socket.io stuff stops working.

This dependencies are in someway conflicting and I have no idea on what do to.

Here's my package.json:

  "dependencies": {
    "express": "3.4.8",
    "jade": "*",
    "share": "^0.6.3",
    "socket.io": "^1.3.5",
    "connect": "*"
  },

Thanks in advance


Solution

  • Managed to solve my problem:

    requirejs([‘ace/ace’, ‘shareJS’, ‘bcsocket’, ‘sharejs_ace’,’jquery’, ‘socket.io’],
       function(ace, sharejs, io) {
    

    And it was passing a bcsocket variable to io

    So when I changed to

    requirejs([‘ace/ace’, ‘shareJS’, ‘socket.io’, ‘bcsocket’, ‘sharejs_ace’,’jquery’],
       function(ace, sharejs, io) {
    

    Then it passed socket.io variable to io.

    It was just a problem with the order.