when troubles come, you can handle it like this
socket.on(`${API_NAME} error`, ({
type, // error constant such as `authenticate user`, `join channel`, etc...
message, // error message
}) => {
// ...
});
users should be authorized to chat
// try to authenticate user
socket.emit('authenticate user', {
username: 'username',
token: 'token',
});
// succeed to authorize user
socket.on('authorized', ({ user }) => {
const {
id, // unique client ID
username, // username
channels, // channels connected
} = user;
});
socket.emit('join channel', {
link: 'channel short ID',
});
// succeed to join channel
socket.on('channel connected', ({ channel }) => {
const {
link, // channel short ID
allUsers, // number of users joined
} = channel;
});
// new user joined (to old connecters)
socket.on('user joined', ({ user, channel }) => {
// ...
});
socket.emit('leave channel', {
link: 'channel short ID',
});
// succeed to leave channel
socket.on('channel disconnected', { channel } => { ... });
// a user dropped out channel
socket.on('user left', { channel, user } => { ... });
send messages to others in the channel and receive messages from them
socket.emit('new message', {
link: 'channel short ID',
content: 'message',
});
socket.on('new message', ({ channel, message }) => {
const {
author, // unique client id
username, // username
content, // message content
} = message;
});