Skip to content

XChat advanced ​

Lower-level client.xchat helpers for when the methods in messages aren't enough.

xchat.token() ​

Mints a short-lived JWT for the chat WebSocket, the token in wss://chat-ws.x.com/ws?token=... (see transport). Returns null when Twitter doesn't issue one.

js
const jwt = await client.xchat.token();

xchat.gql(name, variables?) ​

Calls one of the XChat GraphQL operations emusks knows and returns the raw response. It throws when the response has GraphQL errors. Use it for operations that don't have a helper yet, such as mute and disappearing-message settings.

js
const res = await client.xchat.gql("GetPublicKeys", { ids: ["44196397"], include_juicebox_tokens: true });

The known operations are GetPublicKeys, GenerateXChatTokenMutation, AddEncryptedConversationKeysMutation, SendMessageCreateMutation, AddXChatPublicKeyMutation, DeleteMessageMutation, DeleteXChatPublicKeyMutation, DmAvPermissionsQuery, GetInitialXChatPageQuery, GetConversationPageQuery, GetInboxPageRequestQuery, GetMessageRequestsPageQuery, GetMessageEventsBySequenceIdQuery, AcceptMessageRequestMutation, ConversationDeleteMutation, InitializeXChatMediaUploadMutation, FinalizeXChatMediaUploadMutation, MuteConversationMutation, UnmuteConversationMutation, UpdateConversationTTLMutation and RemoveConversationTTLMutation. The variables for each are in the operations table and the protocol page.

xchat.socket(handlers?) ​

Opens the realtime chat WebSocket and decodes its frames. It sends a keepalive every 30 seconds. Events are decrypted with the conversation keys emusks has already seen, so call read() or inbox() first if you want plaintext.

js
const sock = await client.xchat.socket({
  onEvent: (event) => console.log(event.kind, event.text),
  onTyping: ({ conversationId, senderId }) => console.log(senderId, "is typing in", conversationId),
});

sock.close();
HandlerCalled with
onOpennothing, when the socket opens
onEventevery decoded event, in the same shape as read() messages
onTyping{ conversationId, senderId, activity, at }
onInstructionthe instruction kind, such as a pull or keepalive
onTexttext frames, as strings
onClose(code, reason)

It returns { ws, opened, open, send(bytes), close() }, where opened is a promise that resolves once connected.

not affiliated with X Corp.