!npm socket.io code example

Example 1: install socket.io

npm i socket.io

Example 2: npm socket io

// with npm
npm install socket.io

// with yarn
yarn add socket.io

Example 3: create a web server node js w socket.io

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
  // Ran when a GET request to path '/'
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  // Ran when a socket connected
});

http.listen(3000, () => {
  // Ran when server is ready to take requestes
});

Example 4: socket..io

<script src="https://cdn.socket.io/socket.io-3.0.1.min.js"></script>

Example 5: socket io

npm install --save socket.io

Tags: