Client Side (Socket) - VanillaJS
Client-side code:
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io("urlofyourserver");
// Send the JWT token to the server to authenticate the user
socket.emit("auth", token);
// Send when you want to logout
// socket.emit("unauth");
// Handle user logout when the window is closed
window.addEventListener("beforeunload", () => {
socket.emit("unauth");
});
</script>Last updated