0

I'm using socket.io in total.js and want to use the io object in a module.

how can i access the io object in a module? (pass the object or set the global framework object?)

the initialization code:

require("total.js");

ON("load", function() {
    let io = require("socket.io")(this.server);
});

F.http("debug");
Pourya8366
  • 3,424
  • 4
  • 21
  • 28

1 Answers1

1

First you need to disable WebSocket in Total.js framework: https://docs.totaljs.com/latest/en.html#api~FrameworkConfiguration~allow_websocket

Initialization code for Socket.io:

ON('ready', function() {
    // "IO" will be a global variable, so you can use it everywhere
    global.IO = require('socket.io')(F.server);
});
Peter Sirka
  • 748
  • 5
  • 10
  • I read something about that is very bad to change global object, so now what is the best to do this? – Pourya8366 Jan 15 '19 at 08:37
  • Why is it bad? :-) Total.js uses a lot of global variables and I don't see any problem to create alias for your needs. – Peter Sirka Jan 15 '19 at 11:12
  • i read this article and it tells it's bad because of 3 reasons: https://stackabuse.com/using-global-variables-in-node-js/ – Pourya8366 Jan 15 '19 at 17:23