Sid
Sid

Reputation: 21

where is socket.io/socket.io.js location?

I am making a chat app using socket.io. According to the website documentation, i have to provide a javascript file source which is:

<script src="/socket.io/socket.io.js"></script>

whereas my socket.io folder is under node_modules and on the other hand socket.io folder doesn't even contain socket.io.js, socket-client folder does. I have tried other docs but cannot find any explanation. Can anyone please explain how its working correctly even with the wrong path.

Upvotes: 2

Views: 1017

Answers (1)

Azami
Azami

Reputation: 2161

When you instantiate a socket.io server, it serves the client file at the /socket.io/socket.io.js address automatically without you having to provide anything.

You can thus choose whether you want to import the io from this file, or from the socket.io-client module.

Using the socket.io.js automatically provided by socket.io

<script src="/socket.io/socket.io.js"></script>
<script>
  const socket = io();
</script>

Importing the io object directly with require/import and webpack/browserify

const io = require('socket.io-client');
// or with import syntax
import io from 'socket.io-client';
const socket = io();

Upvotes: 3

Related Questions