@fiveminutes-io/five-minute-chat-contracts
v1.8.1
Published
Five Minute Chat is a SaaS chat service that is very quick to get started with and leverage in your game or app project. This library contains TypeScript interface types for connecting to the Five Minute Chat backends.
Maintainers
Readme
FiveMinutes Chat Client Types
This package provides TypeScript class definitions and helper types for interacting with the FiveMinutes Chat SignalR interface. It includes all necessary message contracts, enums, and data models required to perform a full authentication handshake and exchange chat messages.
Installation
npm install @fiveminutes-io/five-minute-chat-contractsFeatures
- Strict Typing: Full TypeScript definitions for all Request (
Client...) and Response (Server...) objects. - Serialization Helpers: All classes include a pre-configured
$typeproperty (e.g.,FiveMinutes.Model.Messages.Client.ClientChatMessage, Gateway.Contracts) to ensure correct polymorphic deserialization on the server. - Enums: Strongly typed enums for
EventType,Language,UserType, etc.
Usage
Connecting with SignalR
The library is designed to be used in conjunction with @microsoft/signalr. Because the backend uses a polymorphic message pattern, messages are sent as serialized JSON strings via a single hub method (GenericText) and received via a single event (Generic).
1. Setup Connection
import * as signalR from "@microsoft/signalr";
import {
ClientCredentialsResponse,
ClientUserInfoResponse,
ServerWelcome,
ServerCredentialsRequest,
ServerUserInfoRequest
} from "@fiveminutes-io/five-minute-chat-contracts";
// Initialize SignalR Connection
const connection = new signalR.HubConnectionBuilder()
.withUrl("https://signalr.fiveminutes.cloud/signalr")
.withAutomaticReconnect()
.build();2. Handle Incoming Messages
Incoming messages arrive on the "Generic" channel. You must inspect the $type property to determine how to cast and handle the message.
connection.on("Generic", async (message: any) => {
const typeString = message.$type as string;
const className = typeString.split(',')[0].split('.').pop();
switch (className) {
case "ServerCredentialsRequest":
// Handle auth challenge
const credentialsResponse = new ClientCredentialsResponse();
credentialsResponse.ApplicationId = "YOUR_APP_ID";
credentialsResponse.ApplicationSecret = "YOUR_SECRET";
// Send response serialized as JSON
await connection.invoke("GenericText", JSON.stringify(credentialsResponse));
break;
case "ServerUserInfoRequest":
// Handle user info request
const userResponse = new ClientUserInfoResponse();
userResponse.UniqueUserId = crypto.randomUUID();
userResponse.Username = "MyUsername";
await connection.invoke("GenericText", JSON.stringify(userResponse));
break;
case "ServerWelcome":
const welcomeMsg = message as ServerWelcome;
console.log(`Login successful! Display ID: ${welcomeMsg.DisplayId}`);
break;
case "ServerChatMessage":
console.log("New chat message:", message.Content);
break;
default:
console.warn("Unknown message type:", className);
}
});3. Send Messages
To send messages (like chat messages or commands), create an instance of the class and send it via GenericText as a JSON string.
import { ClientChatMessage } from "@fiveminutes-io/five-minute-chat-contracts";
const chatMsg = new ClientChatMessage();
chatMsg.ChannelName = "Global";
chatMsg.Content = "Hello World!";
await connection.invoke("GenericText", JSON.stringify(chatMsg));Build & Test
To build the package locally:
npm run build