Working with Server

If you do not have a backend server, you should override the methods of the Backend service and return promises of client data. In the case of using the built-in Backend you need to specify the token property after the user has successfully logged in.

Backend Service

Backend is a server-side model. It is defined as an object and has methods for sending requests.
Check out the full list of methods.

How to Override Backend Service

How to override Backend:

  • extend it
  • use the override setting of Chat
class MyBackend extends chat.services.Backend {
  // define and override methods here
}
 
webix.ui({
  view: "chat",
  url: "//localhost:3200/",
  override: new Map([
    [chat.services.Backend, MyBackend]
  ])
});

Working with Client Data

If you have client data, you can return them inside of a promise:

class MyBackend extends chat.services.Backend {
  // you can intercept and customize server side calls
  messages(chatId) {
    return Promise.resolve([
      {
        user_id: 1,
        text: "[Custom Message] Working in the offline mode",
      }
    ]);
  }
}

Backend Service methods

As far as Chat uses the web-socket protocol the connection is established once. Also keep in mind that operations like adding chat/message, editing or deleting are performed from the current user perspective.

users()

The method loads users array.

Response data:

The method returns a promise that resolves with an array of user objects with the following structure:

[
  {
    avatar: "remote/avatars/1.jpg",
    email: "alex@brown.com",
    id: 1,
    name: "Alex Brown",
    status: 2
  },
  { .. }
]

chats()

The method loads chats array.

Response data:

The method returns a promise that resolves with an array of chat objects with the following structure:

[
  {
    avatar: "remote/avatars/photo.jpg",
    date: new Date(),
    direct_id: 17,
    id: 52,
    message: "Coffee tonight?",
    name: "PM: Jean Catwright",
    unread_count: 0,
    users: [3, 17]
  },
  { .. }
]

addChat(uid)

The method gets called when the current user creates a private chat.

Parameters:

  • uid (number) - the id of the user the current user creates chat with.

Response data:

The method returns a promise that resolves with the chat object with the following structure:

{
  avatar: "remote/avatars/pm.jpg",
  date: new Date(),
  direct_id: 17,
  id: 52,
  message: "Hi there"
  name: "Today's meeting",
  unread_count: 1,
  users: [3, 17]
}

messages(cid)

The method returns all the messages from a specified chat. Gets called when user enters the chat.

Parameters:

  • cid (number) - the id of the chat to get messages from.

Response data:

The method returns a promise that resolves with an array of message objects that have following structure:

[
 {
   chat_id: 6,
   date: new Date(),
   id: 19,
   text: "Oh, don't bother.. I believe that they can win anyway )",
   user_id: 2
 }
]

addMessage(cid, text, origin)

The method adds a new message to a specified chat.

Parameters:

  • cid (number) - the chat id;
  • text (string) - the message text;
  • origin (number) - client-side id of the message.

Response data:

The method returns the message object with the following structure:

{
  chat_id: 31,
  date: new Date(),
  id: 75,
  text: "Anyone here?",
  user_id: 3
}

removeMessage(messageId)

The method deletes a specified message.

Parameters:

  • messageId (number) - the message id.

The method returns the message object with the following structure:

{
  chat_id: 31,
  date: new Date(),
  id: 71,
  text: "",
  user_id: 0
}

updateMessage(messageId, text)

The method updates a specified message.

Parameters:

  • messageId (number) - the message id;
  • text (string) - the message text.

Response data:

The method returns the message object with the following structure:

{
  chat_id: 31,
  date: new Date(),
  id: 76,
  text: "Hey, Alex :)",
  user_id: 3
}

resetCounter(cid)

The method resets the counter of the undread messages in a specified chat.

Parameters:

  • cid (number) - the chat id.

The method does not return any data.

addGroupChat(name, avatar, users)

The method gets called when a user creates a group chat.

Parameters:

  • name (number) - the chat name;
  • avatar (string) - URL for the chat avatar;
  • users (array) - the ids of the users being invited.

Response data:

The method returns a promise that resolves with the chat object that has the following structure:

{
  avatar: "remote/avatars/picture.jpg",
  date: new Date(),
  direct_id: 0,
  id: 61,
  message: "",
  name: "Hikikomori",
  unread_count: 0,
  users: [4, 5, 3, 1, 87]
}

updateChat(chatId, name, avatar)

The method updates a specified chat.

Parameters:

  • chatId (number) - the chat id;
  • name (string) - the chat name;
  • avatar (string) - URL for the chat avatar.

Response data:

The method returns a promise that resolves with the chat object that has the following structure:

{
  avatar: "remote/avatars/another_picture.jpg",
  date: new Date(),
  direct_id: 0,
  id: 61,
  message: "",
  name: "Renamed", 
  unread_count: 0,
  users: [4, 5, 3, 1, 87]
}

leave(chatId, userId)

The method gets called when a user leaves a specified chat.

Parameters:

  • chatId (number) - the chat id;
  • userId (number) - the user id.

Response data:

The server returns null if the user left successfully.

setUsers(chatId, users)

The method gets called when a user adds/removes members to a specified chat.

Parameters:

  • chatId (number) - the chat id;
  • users (array) - ids of the chat members.

Response data:

The method returns a promise that resolves with the chat object that has the following structure:

{
  avatar: "remote/avatars/another_picture.jpg",
  date: new Date(),
  direct_id: 0,
  id: 61,
  message: "Who knows.. I am rather optimistic.",
  name: "Talks", 
  unread_count: 0,
  users: [4, 5, 3, 1, 87]
}

avatarUploadUrl()

The method returns a url for uploading chat avatars.

Returns:

this.app.config.url + "/chat/0/avatar";
Back to top