§2023-08-10
- Create your GetStarted directory,
$ mkdir socket-chat-example && cd $_
nano package.josn
as
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {}
}
npm instrall express
npm install express@4, he @ symbol is used to indicate the version you want to install.If you omit the version number after @, npm will install the latest version of the package, or just npm install express without @.
$ npm install express@
- package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.18.2"
}
}
- Cresta a index.js
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
/*
* just say Hello world
* app.get('/', (req, res) => {
* res.send('<h1>Hello world</h1>');
* });
*
*/
// Let’s refactor our route handler to use sendFile instead.
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
¶index.html
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; position: fixed; bottom: 0; left: 0; right: 0; display: flex; height: 3rem; box-sizing: border-box; backdrop-filter: blur(10px); }
#input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; }
#input:focus { outline: none; }
#form > button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages > li { padding: 0.5rem 1rem; }
#messages > li:nth-child(odd) { background: #efefef; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
</body>
</html>
¶Integrating Socket.IO
$ npm install socket.io
- package.json
{
"name": "socket-chat-example",
"version": "0.0.1",
"description": "my first socket.io app",
"dependencies": {
"express": "^4.18.2",
"socket.io": "^4.7.2"
}
}
- index.js with socket.io
const http = require('http');
const server = http.createServer(app);
/*
* just say Hello world
* app.get('/', (req, res) => {
* res.send('<h1>Hello world</h1>');
* });
*
*/
// Let’s refactor our route handler to use sendFile instead.
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
/*
// add socket.io
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('a user connected');
});
server.listen(3000, () => {
console.log('listening on *:3000');
});
- index.html has to be modified
Now in index.html add the following snippet before the