-
Previous messages are rendered when user access the page, after it, when user sends a message, user will send it to an API endpoint
POST (/api/v1/messages). -
When a message arrives in that endpoint the
MessageBroadcastJobis started, and responds the client-side immediately with a:no_contentstatus. It uses the job because I think a real-time chat should not block the I/O. -
The
MessageBroadcastJobbroadcasts the message to all users. It uses theMessageService, that will create the message in database and return the rendered message. -
After a message is created the
AttachmentBroadcastJobis started, this job will useAttachmentServiceto look for URLs in the message body. It'll visit each URL to get its contents. -
Again, it's on a job, the message gets immediately back to the user (real-time), and through the job that is created later, user will receive those attachments.
This way we've a real-time asynchronous chat. The user receives every message almost instantaneously and a few microseconds/seconds later, user will see the attachments.
- Ruby 2.3.1
- Rails 5.0.x
- PostgreSQL 9.3
- Redis for production
There's a setup file, so you can just use:
git clone https://github.com/matheusazzi/simple-messenger.git
cd simple-messenger
./bin/setup