shift-chat-mobile-client
v0.1.4
Published
Mobile client for Shift Chat
Downloads
637
Maintainers
Readme
shift-chat-mobile-client
A TypeScript client for integrating mobile applications with the Shift Chat Server. Supports both WebSocket (Socket.IO) for real-time events and HTTP for RESTful operations.
Features
- Real-time Messaging: Send and receive messages instantly via WebSocket.
- Cross-Platform: Works on React Native (Expo), Web, and Node.js.
- Authentication: JWT-based authentication (Bearer token).
- Workspace & Channel Discovery: Fetch workspaces and channels available to the user.
- Message Actions: Reply, Edit, Delete, and React to messages.
Installation
npm install shift-chat-mobile-client
# or
yarn add shift-chat-mobile-clientNote: Access token generation should be handled by your backend integration using the python shift-chat-server-client.
Usage
Initialization
import { ShiftChatMobileClient, ShiftChatEvent } from 'shift-chat-mobile-client';
const client = new ShiftChatMobileClient({
token: "YOUR_JWT_ACCESS_TOKEN"
});Connection
// Connect to the WebSocket server
client.connect();
// Listen for connection events
client.on(ShiftChatEvent.Connect, () => {
console.log('Connected!');
});
client.on(ShiftChatEvent.ConnectError, (err) => {
console.error('Connection failed:', err);
});Discovery
Before sending messages, you usually need to know which channel to send to.
// 1. Get user's workspaces
const workspaces = await client.getWorkspaces();
const myWorkspace = workspaces[0];
// 2. Get channels in that workspace
const channels = await client.getChannels(myWorkspace.id);
const generalChannel = channels.find(c => c.name === 'General');Sending Messages
// Send a simple text message
const message = await client.sendMessage({
channelId: generalChannel.id,
content: "Hello everyone!",
// lexicalJson is optional; a basic paragraph node will be generated if omitted
});
// Reply to a message (Thread)
await client.replyToMessage(message.id, {
channelId: generalChannel.id,
content: "This is a reply in a thread"
});Message Actions
// Edit a message
await client.updateMessage({
messageId: message.id,
content: "Hello everyone! (edited)",
lexicalJson: "..." // Provide updated Lexical JSON string
});
// React to a message (Toggle)
await client.toggleReaction({
messageId: message.id,
emoji: "👍"
});
// Delete a message
await client.deleteMessage(message.id);Listening for Events
// Receive new messages in real-time
client.on(ShiftChatEvent.NewMessage, (msg) => {
console.log(`[${msg.channelId}] ${msg.user?.firstName}: ${msg.content}`);
});
// Receive replies
client.on(ShiftChatEvent.NewReply, (payload) => {
console.log(`New reply to ${payload.rootMessageId}:`, payload.reply.content);
});
// Receive reactions
client.on(ShiftChatEvent.ReactionUpdated, (payload) => {
console.log(`Reactions updated for message ${payload.messageId}:`, payload.reactions);
});API Reference
ShiftChatMobileClient
Connection & Events
| Method | Description |
| :--- | :--- |
| connect() | Establishes WebSocket connection with auth headers. |
| disconnect() | Closes the connection. |
| isConnected() | Returns boolean indicating socket connection status. |
| on(event, handler) | Subscribes to a specific event. |
| off(event, handler) | Unsubscribes from a specific event. |
| emit(event, data) | Emits a raw event to the server. |
Workspaces
| Method | Description |
| :--- | :--- |
| getWorkspaces() | Get workspaces for the user. Returns Promise<Workspace[]> |
| getWorkspace(id) | Get a specific workspace. Returns Promise<Workspace> |
| getWorkspaceUsers(id) | Get members of a workspace. Returns Promise<WorkspaceMember[]> |
Channels
| Method | Description |
| :--- | :--- |
| getChannels(workspaceId?) | Get channels for a user/workspace. Returns Promise<Channel[]> |
| getChannelById(id) | Get a specific channel. Returns Promise<Channel> |
| createGroupChannel(payload) | Creates a group channel. Returns Promise<Channel> |
| createPrivateChannel(payload) | Creates a private channel (DM). Returns Promise<Channel> |
Messages
| Method | Description |
| :--- | :--- |
| getMessages(options) | Fetch messages for a channel. Returns Promise<Message[]> |
| getThread(messageId) | Fetch full thread for a message. Returns Promise<Message[]> |
| sendMessage(payload) | Sends a message. Returns Promise<Message> |
| replyToMessage(rootId, payload) | Sends a thread reply. Returns Promise<Message> |
| updateMessage(payload) | Edits a message. Returns Promise<Message> |
| toggleReaction(payload) | Toggles emoji reaction. Returns Promise<{ success: boolean; added: boolean }> |
| deleteMessage(id) | Deletes a message. Returns Promise<void> |
Events (ShiftChatEvent)
Connect: Socket connectedDisconnect: Socket disconnectedNewMessage: A new message was posted in a subscribed channelNewReply: A reply was posted to a threadMessageUpdated: A message content was editedReactionUpdated: Reactions on a message changedNewNotification: User was mentioned or notified
