title: How to display message when user disconnects from chat room in socket.io
body: <p>I am working on a project in which there is a chat room. The messaging and the joining message is working fine but I don't know how to display a message when someone disconnects. I don't have much knowledge about socket.io as you will see in the code below.</p>
  <p>(Edit)</p>
  <p>Server Code(Express/Node)</p>
  <pre><code>    io.on('connection', socket =&gt; {
    socket.on('message', ({ name, message }) =&gt; {
      io.emit('message', { name, message })
    })
    socket.on('join' , (name)=&gt;{
      console.log(name);
      socket.broadcast.emit(&quot;join&quot; , name.usnm.toUpperCase() + &quot; just joined&quot;)
    })
    socket.on('pre_disconnect', (name) =&gt; {
      console.log(name);
      socket.broadcast.emit(&quot;pre_disconnect&quot; , name.usnm.toUpperCase() + &quot; just left&quot;)
    })
  })
  </code></pre>
  <p>Client Code(React.js)</p>
  <pre><code>const socketRef = useRef()
  useEffect(
      () =&gt; {
          socketRef.current = io.connect(&quot;http://localhost:4000&quot;)
          
          socketRef.current.on(&quot;message&quot;, ({ name, message }) =&gt; {
              setChat([ ...chat, { name, message } ])
          })
          socketRef.current.on(&quot;join&quot;, (usnm) =&gt; {
              setChat([ ...chat, {name:usnm} ])
          })
          socketRef.current.on(&quot;pre_disconnect&quot;, (usnm) =&gt; {
          setChat([ ...chat, {name:usnm} ])
      })
      },
      [ chat ]
  )
  useEffect(
      () =&gt; {
          let usnm = sessionStorage.getItem(&quot;User&quot;);
          socketRef.current.emit(&quot;join&quot; ,{usnm});
          return () =&gt; {
        socketRef.current.emit(&quot;pre_disconnect&quot; ,{usnm});
        socketRef.current.disconnect()
      }
      },
      [  ]
  )
  </code></pre>
  <p>In the code above the <code>pre_disconnect</code> doesn't do anything. Code works the same with or without it.</p>