Events

When you are connected to a room you can receive different events:

Don’t forget

Don’t forget default socket.io events.

The server should return a single message using the following format.

// typescript example
interface SocketResponse<T>{
  msg: string,
  data: T // Notice that data is generic it means that can be any type of data it depends on the event that emits the response
}

Initial response

  • initial-response: This event is sent only to a new connected user. The provided data is an array of users IDs. These are the IDs of the connected users including you.

Example response

{
  "msg": "Connected users",
  "data": [
    {
      "ID": 1,
      "role": "TEACHER",
      "name": "Sherley Herminia"
    },
    {
      "ID": 2,
      "role": "STUDENT",
      "name": "Kiki Gunþīharjaz"
    },
    {
      "ID": 3,
      "role": "STUDENT",
      "name": "Devora Khushi"
    }
  ]
}

User connected

  • user-connected: This event is sent to users who already was connected to room but no to new connected user. It provides information about the new connected user.

Example response

{
  "msg": "New user connected",
  "data": {
    "ID": 6,
    "role": "<user role>", // Role could be: ADMIN, TEACHER or STUDENT
    "name": "<user name>"
  }
}

User disconnected

  • user-disconnected: This event is sent to users connected to a room every time a user leaves a room. It provides information about the disconnected user.

Example response

{
  "msg": "User disconnected",
  "data": {
    "ID": 6,
    "role": "<user role>", // Role could be: ADMIN, TEACHER or STUDENT,
    "name": "<user name>"
  }
}

Message

  • message: This event is emitted every time that you receive a message.

Example respons

{
  "msg": "New message",
  "data": {
    "from": 3, // User id where message comes from
    "message": {
      "value": "this si the message content",
      "type": "text"
    }
  }
}
Don’t forget

Message type is not restricted to any type. It can text, image, video, potato, etc. Define your own types and your custom values to allow send complex messages. Value can be an object, a boolea, string, number, etc.

Allowed events to emit

Messages

This event is used to send a message to a user in your room.

Example

const message = {
  type: 'text',
  value: 'A simple message'
};

const userID = 3; // This user id depends on which users are connected to your room

socket.emit('message', message, userID);

Broadcast Message

This event is used to send a message to all users in a room except you.

const message = {
  type: 'text',
  value: 'A simple message'
};

socket.emit('broadcast-message', message);