Optimize event retrieval query, and implement chat