react 前端框架如何驱动企业数字化转型与创新发展
697
2022-10-13
ChatApp一个使用Django构建的小型点对点消息中心应用程序
ChatApp
A small functional person-to-person message center application built using Django. It has a REST API and uses WebSockets to notify clients of new messages and avoid polling.
Architecture
When a user logs in, the frontend downloads the user list and opens a Websocket connection to the server (notifications channel).When a user selects another user to chat, the frontend downloads the latest 15 messages (see settings) they've exchanged.When a user sends a message, the frontend sends a POST to the REST API, then Django saves the message and notifies the users involved using the Websocket connection (sends the new message ID).When the frontend receives a new message notification (with the message ID), it performs a GET query to the API to download the received message.
Scaling
Requests
"Because Channels takes Django into a multi-process model, you no longer run everything in one process along with a WSGI server (of course, you’re still free to do that if you don’t want to use Channels). Instead, you run one or more interface servers, and one or more worker servers, connected by that channel layer you configured earlier."
In this case, I'm using the In-Memory channel system, but could be changed to the Redis backend to improve performance and spawn multiple workers in a distributed environment.
Please take a look at the link below for more information: https://channels.readthedocs.io/en/latest/introduction.html
update 04/06/19
using pipenv for package managementmove to Channels 2use redis as the channel layer backing store. for more information, please check channels_redis
Database
For this demo, I'm using a simple MySQL setup. If more performance is required, a MySQL cluster / shard could be deployed.
PD: I'm using indexes to improve performance.
Assumptions
Because of time constraints this project lacks of:
User Sign-In / Forgot PasswordUser Selector PaginationGood Test CoverageBetter Comments / Documentation StringsFrontend TestsModern Frontend Framework (like React)Frontend Package (automatic lintin, building and minification)Proper UX / UI design (looks plain bootstrap)
Run
move to project root folder Create and activate a virtualenv (Python 3)
pipenv --python 3 shell
Install requirements
pipenv install
Create a MySQL database
CREATE DATABASE chat CHARACTER SET utf8;
Start Redis Server
redis-server
Init database
./manage.py migrate
Run tests
./manage.py test
Create admin user
./manage.py createsuperuser
Run development server
./manage.py runserver
To override default settings, create a local_settings.py file in the chat folder.
Message prefetch config (load last n messages):
MESSAGES_TO_LOAD = 15
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~