Skip to main content

Event Name

leave-pr-room

Description

Leave a WebSocket room for a specific pull request. After leaving, you’ll no longer receive real-time notifications for that pull request room.

Connection Setup

First, establish a WebSocket connection to the Diffy API:
import { io } from 'socket.io-client';

const socket = io('https://api.diffy.com', {
  transports: ['websocket']
});

Payload

pullRequestId
number
required
The ID of the pull request room to leave
username
string
required
The username of the user leaving the room

Request Example

socket.emit('leave-pr-room', {
  pullRequestId: 123,
  username: 'johndoe'
});

Server Responses

When a user leaves a PR room, the server emits multiple events to all remaining clients in the room:

left Event

Broadcast to all remaining users in the room when someone leaves.
message
string
Notification message indicating who left and from which PRExample: "johndoe left PR 123"

user-count Event

Sent to all remaining users in the room with the updated user count.
userCount
number
The total number of users still in the room (after the user left)

active-users Event

Sent to all remaining users in the room with the updated list of active usernames.
users
array
Array of usernames currently remaining in the room

Listening for Responses

// Listen for leave notifications
socket.on('left', (message) => {
  console.log(message); // "johndoe left PR 123"
});

// Listen for user count updates
socket.on('user-count', (data) => {
  console.log(`Users remaining: ${data.userCount}`);
});

// Listen for updated active users list
socket.on('active-users', (users) => {
  console.log('Active users:', users);
});

Complete Example

import { io } from 'socket.io-client';

const socket = io('https://api.diffy.com', {
  transports: ['websocket']
});

socket.on('connect', () => {
  console.log('Connected to WebSocket server');
  
  // First join the room
  socket.emit('join-pr-room', {
    pullRequestId: 123,
    username: 'johndoe'
  });
  
  // Leave after some time
  setTimeout(() => {
    socket.emit('leave-pr-room', {
      pullRequestId: 123,
      username: 'johndoe'
    });
  }, 30000); // Leave after 30 seconds
});

// Listen for room events
socket.on('left', (message) => {
  console.log('Leave notification:', message);
});

socket.on('user-count', (data) => {
  console.log('User count:', data.userCount);
});

socket.on('active-users', (users) => {
  console.log('Active users:', users);
});

Room Architecture

Diffy uses a room-based architecture where each pull request has its own dedicated room. The room identifier follows the pattern pr:{pullRequestId}. When you leave a room, you’re removed from that specific pull request’s channel.

Notes

  • Users are automatically removed from the room upon emitting this event
  • All remaining users in the room receive notifications when someone leaves
  • The user count and active users list are updated for all remaining participants
  • Leaving a room does not disconnect you from the WebSocket server; you remain connected and can join other rooms
  • It’s good practice to leave rooms when navigating away from a pull request view

Build docs developers (and LLMs) love