Skip to main content

startTyping

Show typing indicator in a chat to let participants know you’re composing a message.
await sdk.chats.startTyping(chatGuid): Promise<void>

Parameters

chatGuid
string
required
Unique identifier of the chat to show typing indicator in

Returns

Returns a Promise that resolves when the typing indicator is activated.

Example

import { SDK } from '@photon-ai/advanced-imessage-kit';

const sdk = SDK({ serverUrl: 'http://localhost:1234' });

const chatGuid = 'iMessage;+;chat123456';

// Start typing indicator
await sdk.chats.startTyping(chatGuid);
console.log('Typing indicator started');

// Simulate composing a message
await new Promise(resolve => setTimeout(resolve, 3000));

// Send the message
await sdk.messages.send({
  chatGuid,
  message: 'Hello!'
});

// Stop typing indicator
await sdk.chats.stopTyping(chatGuid);

stopTyping

Hide typing indicator in a chat.
await sdk.chats.stopTyping(chatGuid): Promise<void>

Parameters

chatGuid
string
required
Unique identifier of the chat to hide typing indicator in

Returns

Returns a Promise that resolves when the typing indicator is deactivated.

Example

import { SDK } from '@photon-ai/advanced-imessage-kit';

const sdk = SDK({ serverUrl: 'http://localhost:1234' });

const chatGuid = 'iMessage;+;chat123456';

// Stop typing indicator
await sdk.chats.stopTyping(chatGuid);
console.log('Typing indicator stopped');

Advanced Usage

import { SDK } from '@photon-ai/advanced-imessage-kit';

const sdk = SDK({ serverUrl: 'http://localhost:1234' });

const chatGuid = 'iMessage;+;chat123456';

// Function to handle typing with automatic cleanup
async function composeMessage(chatGuid: string, messageText: string) {
  try {
    // Start typing indicator
    await sdk.chats.startTyping(chatGuid);
    
    // Simulate message composition time
    const typingDelay = messageText.length * 50; // 50ms per character
    await new Promise(resolve => setTimeout(resolve, typingDelay));
    
    // Send message
    await sdk.messages.send({
      chatGuid,
      message: messageText
    });
  } finally {
    // Always stop typing indicator, even if sending fails
    await sdk.chats.stopTyping(chatGuid);
  }
}

// Use the function
await composeMessage(chatGuid, 'This is a longer message that takes time to type');

// Example with user input handling
let typingTimeout: NodeJS.Timeout | null = null;

function handleUserTyping(chatGuid: string, isTyping: boolean) {
  if (isTyping) {
    // User started typing
    sdk.chats.startTyping(chatGuid);
    
    // Reset timeout
    if (typingTimeout) {
      clearTimeout(typingTimeout);
    }
    
    // Auto-stop typing after 5 seconds of inactivity
    typingTimeout = setTimeout(() => {
      sdk.chats.stopTyping(chatGuid);
    }, 5000);
  } else {
    // User stopped typing
    if (typingTimeout) {
      clearTimeout(typingTimeout);
    }
    sdk.chats.stopTyping(chatGuid);
  }
}

// Simulate user typing events
handleUserTyping(chatGuid, true);  // User starts typing
handleUserTyping(chatGuid, false); // User stops typing

Build docs developers (and LLMs) love