-4

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.3.5/socket.io.js"></script>

How to include external library in node.js.

I need to include the below library in nodejs client file(.js file).

RAMKUMAR
  • 1
  • 3
  • have you installed in your project using `npm` or `bower` ? – Aatif Bandey Sep 27 '16 at 08:56
  • 2
    Seems a duplicate of this post : http://stackoverflow.com/questions/23641631/how-to-import-external-library-to-nodejs – P.Mantello Sep 27 '16 at 08:58
  • Yes aatif-bandey. In my Nodejs Server i have installed npm. Nodejs Client running on a device. There is some limitation in client side to install NPM. So i'm going to include libraries in Client end. – RAMKUMAR Sep 27 '16 at 09:10

4 Answers4

1

Socket.io comes with two components, an NPM module and a client-side script.

Install the NPM module but running this in the console:

npm install socket.io

Docs: https://www.npmjs.com/package/socket.io

And then put the client-side script inside your view template, see docs: http://socket.io/download/

Tom
  • 2,543
  • 3
  • 21
  • 42
0

you can download it and require it locally (require('./socket.io.js')). The best way would be to find it on npm and install it like any other node module using the npm install command and then require it like require('npmModuleName')

For your file here is the npm link: https://www.npmjs.com/package/socket.io.

You can install it like npm install socket.io and use it like

var io = require('socket.io')();
io.on('connection', function(socket){});
io.listen(3000);
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

Simple explaination: nodejs uses a module system. This means that you can use the command line to include new modules in your application:

npm init
npm install socket.io 

This will make a package.json and node_modules in your project and now you can include the module.

In your application you can use the installed module with a require statement

var io = require(socket.io)

Now you can just use socket.io like you did before, but before you continue you might want to read up into nodejs, modules and npm.

https://www.npmjs.com/package/socket.io

https://nodejs.org/api/modules.html

jimmy jansen
  • 647
  • 4
  • 12
0

It depends on your needs.

Socket.IO consists of two parts: Server API and Client API

To install Server API part you need to install it using NPM. Installation is pretty simple:

npm install socket.io --save

And then import it, where you need (example for ES6):

import IO from 'socket.io'
let socket = IO(`http://localhost:8000`)

For client side, there is few options.

Load and include from CDN:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

If you are using bower, you can install socket.io-client:

bower install socket.io-client --save

and then import it like this:

<script src="/bower_components/socket.io-client/socket.io.js"></script>

Also you can download this file directly to your project and import it, but it won't a best way...

Dmytro Medvid
  • 4,988
  • 3
  • 25
  • 29