Skip to content

Client Connect and Disconnect Events

Video Lecture

Client Connect and Disconnect Events Client Connect and Disconnect Events

Description

Client Side Connect and Disconnect events.

./src/client/client.ts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import { io } from 'socket.io-client'

const socket = io()

socket.on('connect', () => {
  document.body.innerText = 'Connected : ' + socket.id
})

socket.on('disconnect', (message) => {
  alert('Disconnected from Server \nReason : ' + message)
})

socket.on('message', (message) => {
  document.body.innerHTML += '<p>' + message + '</p>'
  window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' })
})