Context: I'm trying to create a live editor. So that multiple users can see the changes live. As an editor, I'm using react-quill. And for emitting data, I'm using socket.
Idea:
- While editing from the client emits the changes.
- Socket server will receive the changes and emit for all connections.
- the client will receive the emitted change and show it up on all editor.
Problem: While typing(quickly) on the editor it automatically emitting the content.
Client side:
export const QuillEditor = () => {
...
...
const [value, setValue] = useState('');
socket.on('emittedText', ({ content }) => {
setValue(content);
});
const emitEditorData = (content: any) => {
socket.emit('editorText', { content: content, id: id });
};
return (
<>
<ReactQuill
style={{ height: 300 }}
placeholder="Type and share..."
theme="snow"
value={value}
onChange={(content, delta, editor) => {
emitEditorData(content);
}}
/>
</>
);
};
Server side:
// handle socket connections and emits
io.on('connection', function(socket) {
console.log('a user connected');
socket.on('editorText', ({ content, id }) => {
//id for group data store
editorText[id] = content;
io.emit('emittedText', { content });
});
});
Is it a react-quill issue or I'm doing wrong?