Events

Events

Ncloud Chat provides the Event Listener function, which can handle diverse events that occur from the client side. With this function, you can monitor many circumstances occurring in the chat application in real time and respond to them properly. The following describes the main events and how to handle events.

Main event types

  1. Receive message: triggered when a new message is received.

  2. Delete message: triggered when a message is deleted.

  3. Error message: triggered when an error occurs.

  4. Connected: triggered when successfully connected to the server.

  5. Disconnected: triggered when disconnected from the server.

  6. Typing start and end: triggered when you start or end typing.

  7. Add and remove members: triggered when a user is added or removed.

  8. Member suspension and withdrawal: triggered when a user gets suspended in the channel or withdraws from the channel.

The following shows how to listen for events on the client side.

nc.dispatcher.onMessageReceived += message =>
{
    Console.WriteLine("received a new message: ", message);
}

Connect and disconnect event handler

With event handlers, you can receive diverse events, and implement the logic you need. The following codes show how to connect and disconnect the event handler for each event.

// Receive message
nc.dispatcher.onMessageReceived += e =>
{
    Console.WriteLine("onMessageReceived: ", e);
};

// Delete message
nc.dispatcher.onMessageDeleted += e =>
{
    Console.WriteLine("onMessageDeleted: ", e);
};

// Error message
nc.dispatcher.onErrorReceived += e =>
{
    Console.WriteLine("[CloudChatSample] onErrorReceived: ", e);
};

// Connected
nc.dispatcher.onConnected += e =>
{
    Console.WriteLine("[CloudChatSample] Connected to server with id: {0} ", e);
};

// Disconnected
nc.dispatcher.onDisconnected += e =>
{
    Console.WriteLine("Disconnected");
};

// When typing starts
nc.dispatcher.onStartTyping += e =>
{
    Console.WriteLine("onStartTyping: ", e);
};

// When typing ends
nc.dispatcher.onStopTyping += e =>
{
    Console.WriteLine("onStopTyping: ", e);
};

// When a user subscribes to a channel
nc.dispatcher.onMemberAdded += e =>
{
    Console.WriteLine("onMemberAdded: ", e);
};

// When a user unsubscribes from a channel
nc.dispatcher.onMemberLeft += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberLeft: ", e);
};

// When a user gets suspended from a channel
nc.dispatcher.onMemberBanned += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberBanned: ", e);
};

// When a user withdraws from a channel
nc.dispatcher.onMemberDeleted += e =>
{
    Console.WriteLine("[CloudChatSample] onMemberDeleted: ", e);
};

With Event Listener, the user of Ncloud Chat can identify the change of the chat environment in real time and respond to it properly.

Last updated