1// npm i socket.io
2const http = require('http')
3const express = require('express')
4const socketio = require('socket.io')
5
6const app = express()
7const server = http.createServer(app)
8const io = socketio(server)
9
10const port = process.env.PORT || 3000
11
12server.listen(port, () => {
13 console.log(`Server is up on port ${port}!`)
14})
1var app = require('express')();
2var http = require('http').createServer(app);
3var io = require('socket.io')(http);
4
5app.get('/', (req, res) => {
6 // Ran when a GET request to path '/'
7 res.sendFile(__dirname + '/index.html');
8});
9
10io.on('connection', (socket) => {
11 // Ran when a socket connected
12});
13
14http.listen(3000, () => {
15 // Ran when server is ready to take requestes
16});
1<script src="/socket.io/socket.io.js"></script>
2<script>
3 const socket = io();
4</script>
1const express = require("express");
2const http = require("http");
3const socketIo = require("socket.io");
4const port = process.env.PORT || 8001;
5const index = require("./routes/index");
6const app = express();
7app.use(index);
8const server = http.createServer(app);
9const io = socketIo(server); // < Interesting!
10const getApiAndEmit = "TODO";
1const socket = io('ws://localhost:3000');
2socket.on('connect', () => {
3 // either with send()
4 socket.send('Hello!');
5 // or with emit() and custom event names
6 socket.emit('salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from([1, 2, 3, 4]));});
7// handle the event sent with socket.send()
8socket.on('message', data => {
9 console.log(data);
10});
11// handle the event sent with socket.emit()
12socket.on('greetings', (elem1, elem2, elem3) => {
13 console.log(elem1, elem2, elem3);
14});