0

Im leaning towards this not being possible, based on all my searching prior to posting this (unless using jquery Can I use multiple versions of jQuery on the same page? which i am not) but I figured I would check.

I have come accross a situation where I need one version of socket.io in order for my web application to work, and another version in order to communicate with my python websockets.

Is it possible to use multiple versions of the same js? for instance, one remote, one local:

<script src="/socket.io/socket.io.js" as="ioA"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js" as="ioB"></script>

Obviously that itself isnt going to work but I hope it helps illustrate what I want.

If my fear is true that this isnt doable, is there a workaround? Such as containing what I need from the remote one in another js/ejs file, and somehow calling to it from the first page? or will the imports clobber each other.

Apologies if this is a dumb question - haven't come accross this in my limited use of these languages, and can't get a straight answer from my own searches

aescript
  • 1,775
  • 4
  • 20
  • 30
  • i think it is possible, what is wrong when you use multiple js? – waiaan May 22 '20 at 03:11
  • I have to include script.io twice. I have one socket that connects to a websocket from this package - which only works when I use my local socket.io and I have another that connects to a Python websocket which requires a different version of socket.io to connect. So when I have both of those script tags, whatever comes last will work and the other socket stops working. Unfortunately I can't change versions in my node project because of other dependencies – aescript May 22 '20 at 03:23
  • when you include the first socket.io, you can use a variable to store it globally. – waiaan May 22 '20 at 03:28
  • or change the global name in your local js – waiaan May 22 '20 at 03:30
  • How would I use a variable to store it? That sounds promising. I'm also not sure how to change the global name of my local one but I'm guessing I can google it – aescript May 22 '20 at 03:32

1 Answers1

1

maybe it would work

<script src="/socket.io/socket.io.js" as="ioA"></script>
<script>
  window.antherIO=io;// io refer to the name above.
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js" as="ioB"></script>

and the local one maybe like this:

function (f) {
    if (typeof exports === "object" && typeof module !== "undefined") {
        module.exports = f()
    } else if (typeof define === "function" && define.amd) {
        define([], f)
    } else {
        var g;
        if (typeof window !== "undefined") {
            g = window
        } else if (typeof global !== "undefined") {
            g = global
        } else if (typeof self !== "undefined") {
            g = self
        } else {
            g = this
        }
        g.io = f()
    }
})(function () {
...

g.io = f()

io set to another name.

waiaan
  • 210
  • 1
  • 4
  • I'll give this a try shortly and let you know. In case it works you may want to remove my broken "as" parameter from the tag for a more correct top answer – aescript May 22 '20 at 03:37