Skip to content

Javascript (server)

The @ezchat/server npm package provides a client library for interacting with the EZChat server. This library allows you to easily manage chatters, rooms, and messages, as well as retrieve access tokens for chatters. The library is written in TypeScript and is compatible with both TypeScript and JavaScript projects.

Installation

npm install @ezchat/server

Authentication

An important piece needed of your server is handling chatter authentication. Basically, you need to authenticate that user, and then request an access token for that user. This token can be passed back and used by that user to connect to a room as shown in this diagram. An example of how to do this with express is below:

import { EZChatClient } from "@ezchat/server";
import express from "express";

const app = express();
const client = new EZChatClient(<API KEY>);



app.get("/auth", yourAuthenticationMiddlewareHere(), async (req, res) => {
  const ezChatChatterId = req.chatterIdReturnedFromMiddleware;
  const token = await client.getToken(ezChatChatterId);
  res.send(token);
});

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Example

import { EZChatClient } from "@ezchat/server";

const client = new EZChatClient(<API KEY>);

const room = await client.createRoom();
const room2 = await client.getRoom(room.id);

EZChatClient Class

Constructor

constructor(apiKey: string)

  • Description: Initializes a new instance of the EZChatClient class with the provided API key.
  • Parameters:
  • apiKey (string): The API key used for authentication.

getToken

getToken(chatterId: number): Promise<any>

  • Description: Retrieves the access token for a specific chatter.
  • Parameters:
  • chatterId (number): The ID of the chatter.
  • Returns: A Promise that resolves with the access token.

getChatter

getChatter(chatterId: number): Promise<any>

  • Description: Retrieves information about a specific chatter.
  • Parameters:
  • chatterId (number): The ID of the chatter.
  • Returns: A Promise that resolves with the chatter information.

getAllChatters

getAllChatters(page?: number, size?: number): Promise<any>

  • Description: Retrieves a list of all chatters.
  • Parameters:
  • page (optional, number): The page number for pagination.
  • size (optional, number): The number of items per page.
  • Returns: A Promise that resolves with the list of chatters.

createChatter

createChatter(chatterName: string): Promise<any>

  • Description: Creates a new chatter with the specified name.
  • Parameters:
  • chatterName (string): The name of the chatter to create.
  • Returns: A Promise that resolves with the created chatter.

deleteChatter

deleteChatter(chatterId: number): Promise<any>

  • Description: Deletes a chatter with the specified ID.
  • Parameters:
  • chatterId (number): The ID of the chatter to delete.
  • Returns: A Promise that resolves when the chatter is successfully deleted.

getMessage

getMessage(messageId: number): Promise<any>

  • Description: Retrieves information about a specific message.
  • Parameters:
  • messageId (number): The ID of the message.
  • Returns: A Promise that resolves with the message information.

deleteMessage

deleteMessage(messageId: number): Promise<any>

  • Description: Deletes a message with the specified ID.
  • Parameters:
  • messageId (number): The ID of the message to delete.
  • Returns: A Promise that resolves when the message is successfully deleted.

createRoom

createRoom(roomStatus?: string, roomType?: string): Promise<any>

  • Description: Creates a new room with optional status and type.
  • Parameters:
  • roomStatus (optional, string): The status of the room.
  • roomType (optional, string): The type of the room.
  • Returns: A Promise that resolves with the created room.

getRoom

getRoom(roomId: number): Promise<any>

  • Description: Retrieves information about a specific room.
  • Parameters:
  • roomId (number): The ID of the room.
  • Returns: A Promise that resolves with the room information.

updateRoom

updateRoom(roomId: number, roomType?: string, roomStatus?: string): Promise<any>

  • Description: Updates the type or status of a room.
  • Parameters:
  • roomId (number): The ID of the room to update.
  • roomType (optional, string): The new type of the room.
  • roomStatus (optional, string): The new status of the room.
  • Returns: A Promise that resolves with the updated room.

getChattersInRoom

getChattersInRoom(roomId: number): Promise<any>

  • Description: Retrieves a list of chatters in a specific room.
  • Parameters:
  • roomId (number): The ID of the room.
  • Returns: A Promise that resolves with the list of chatters in the room.

addChatterToRoom

addChatterToRoom(roomId: number, chatterId: number): Promise<any>

  • Description: Adds a chatter to a specific room.
  • Parameters:
  • roomId (number): The ID of the room.
  • chatterId (number): The ID of the chatter to add.
  • Returns: A Promise that resolves when the chatter is successfully added to the room.

removeChatterFromRoom

removeChatterFromRoom(roomId: number, chatterId: number): Promise<any>

  • Description: Removes a chatter from a specific room.
  • Parameters:
  • roomId (number): The ID of the room.
  • chatterId (number): The ID of the chatter to remove.
  • Returns: A Promise that resolves when the chatter is successfully removed from the room.