Published
Edited
Jun 20, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
send = async payload => {
await socket.emit('chat', payload);
}
Insert cell
onFormSubmit = async e => {
if (e.preventDefault) e.preventDefault(); // prevents page reloading
const m = e.target.childNodes[0].value;
if (m !== "") {
const payload = {
userId: myIdentifier,
message: m,
time: Date.now()
};
e.target.childNodes[0].value = "";
appendToTextarea(payload);
await send(payload);
}
return false;
}
Insert cell
appendToTextarea = data => {
// the data should be validated when parsed
const m = data.message;
const t = data.time;
const userId = data.userId;

const newLine = `\n${userId} ${new Date(t).toLocaleTimeString()} - ${m}`;
textarea.value += newLine;
}
Insert cell
function onDataReceived(data) {
appendToTextarea(data);
}
Insert cell
io = require('socket.io-client@2.3.0/dist/socket.io.dev.js')
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more