<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).
<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).
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/
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);
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.
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...