Rewrite chat.js in backend

Refactorisation and simplification.
Replace all $.ajax()