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.
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.
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.
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();| Handler | Called with |
|---|---|
onOpen | nothing, when the socket opens |
onEvent | every decoded event, in the same shape as read() messages |
onTyping | { conversationId, senderId, activity, at } |
onInstruction | the instruction kind, such as a pull or keepalive |
onText | text frames, as strings |
onClose | (code, reason) |
It returns { ws, opened, open, send(bytes), close() }, where opened is a promise that resolves once connected.