@web-engine-dev/social
Social features for game platforms including friends, chat, presence, and parties.
Features
- Friends List: Add, remove, block friends
- Real-Time Chat: Direct and group messaging
- Presence: Online status and activity
- Parties: Group players for matchmaking
- Invites: Game session invitations
Installation
bash
npm install @web-engine-dev/social
# or
pnpm add @web-engine-dev/socialQuick Start
typescript
import { SocialService } from '@web-engine-dev/social';
const social = new SocialService({
apiUrl: 'https://api.platform.com',
userId: currentUser.id,
});
// Get friends
const friends = await social.getFriends();
// Send friend request
await social.sendFriendRequest(userId);
// Set presence
social.setPresence({
status: 'online',
activity: 'Playing Level 5',
});
// Send message
await social.sendMessage(friendId, 'Want to play?');API Overview
Friends
typescript
// Get friends list
const friends = await social.getFriends();
// Friend requests
await social.sendFriendRequest(userId);
await social.acceptFriendRequest(requestId);
await social.declineFriendRequest(requestId);
// Get pending requests
const incoming = await social.getIncomingRequests();
const outgoing = await social.getOutgoingRequests();
// Remove/block
await social.removeFriend(userId);
await social.blockUser(userId);
await social.unblockUser(userId);Chat
typescript
// Direct message
await social.sendMessage(userId, 'Hello!');
// Get conversation
const messages = await social.getMessages(userId, {
limit: 50,
before: timestamp,
});
// Real-time messages
social.onMessage((message) => {
console.log(`${message.from}: ${message.text}`);
});
// Group chat
const group = await social.createGroup(['user1', 'user2']);
await group.sendMessage('Hey everyone!');Presence
typescript
// Set status
social.setPresence({
status: 'online', // 'online', 'away', 'dnd', 'offline'
activity: 'In Game',
gameId: 'my-game',
});
// Get friend presence
const presence = await social.getPresence(friendId);
console.log(presence.status);
console.log(presence.activity);
console.log(presence.lastSeen);
// Listen for changes
social.onPresenceChange(friendId, (presence) => {
updateFriendStatus(friendId, presence);
});Parties
typescript
// Create party
const party = await social.createParty();
// Invite friends
await party.invite(friendId);
// Join party
await social.joinParty(inviteCode);
// Party chat
party.onMessage((msg) => console.log(msg));
await party.sendMessage('Ready to play?');
// Start matchmaking as party
await matchmaking.queue({ partyId: party.id });