refactored js code to display the chat message instantly