let socket=io();
socket.on(‘connected’,()=>{
console.log(“conected”+socket.id)
})
$(function(){
let msglist=$(’#msg_list’)
let sendbtn=$(’#msg_btn’)
let msgbox=$(’#msg_box’);
sendbtn.click(function(){
let msg=msgbox.val()
socket.emit('send_msg',{massage:msg})
})
socket.on('rcv_msg',function(data){
msglist.append( ("<li>"+ data.massage +"</li>"))
})
})
in server.js
const express=require(‘express’)
const path= require(‘path’)
const socketio=require(‘socket.io’)
const http=require(‘http’)
const app=express();
const server=http.createServer(app)
const io=socketio(server)
app.use(’/’,express.static(path.join(__dirname,‘frontend’)))
io.on(‘connection’,(socket)=>{
console.log(“new socket created”+socket.id)
socket.emit(‘connected’)
socket.on(‘send_msg’,(data)=>{console.log(“recived maassage=”+data.massage)
io.emit(‘rcv_msg’,data)})
})
server.listen(2345,()=>console.log(‘website open on http://localhost:2345’))
in index.html
Page Title