require socket.io code example
Example 1: install socket.io
npm i socket.io
Example 2: socket io script
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
</script>
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) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
});
http.listen(3000, () => {
});
Example 4: socket.io documentation
const socket = new WebSocket('ws://localhost:3000');socket.onopen(() => { socket.send('Hello!');});socket.onmessage(data => { console.log(data);});