Chat rooms with Swoole+React

Keywords: PHP React github npm Laravel

Front-end separated projects, chat rooms implemented by Swoole+React, the framework structure of the entire project can be referenced, front-end react+react-redux+react-router+react-ant, etc. Background uses easySwoole to implement the middleware (data encapsulation, token verification, signature verification) by itself, and carefully look at the code to learn a lot,?!

1. Project Links

 

1.1 swoole (please star)

https://github.com/LaravelChen/swoole_chat...

 

1.2 react (please star)

https://github.com/LaravelChen/React-Small...

 

1.3 api framework (basic requirements are fully met, can you try it yourself?)

https://github.com/LaravelChen/swoole_api_...
Performance display (strong, strong, strong)

 

2. Introduction

In order to make the development more convenient, I implemented the middleware by myself, encapsulated the request data body, used jwt to implement token validation of api, integrated ORM of Laravel, encapsulated a set of data request process suitable for API writing process again, can see the Model class under App/Base directory specifically, detailed development steps can be seen in the code.

 

3. Main implementation

  • Sign in, send the authentication code (you can print it out with front-end react if you need to test it)
  • Public chat rooms (once a user logs in, the list of users increases and the user can add friends)
  • Message Push (can be achieved using swoole's asynchronous process)
  • Private chat room (add good friends to have a private chat)
  • The rest of the functionality can be added...

 

4. Installation

 

4.1 Background Installation

This is just background logic. Please move the corresponding items from the front to: https://github.com/LaravelChen/React-Small...

php server start

Because swoole resides in memory, it needs to be restarted once the code is modified.

 

4.2 Front End Installation

npm install
npm run start

 

5. Project effectiveness

5.1 Chat Room

5.2 Private Chat Room

 

 

 

In addition, there are other friends, the effect of message push is not demonstrated, you can download and install it yourself, the effect is good!

6. Posman interface reference

https://www.getpostman.com/collections/7f9...

 

7. Data Table Structure

1. Database name

swoole_framework

chat_content table

 1 CREATE TABLE `chat_content` (
 2   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
 3   `user_id` int(11) NOT NULL COMMENT 'user id',
 4   `to_user_id` int(11) DEFAULT NULL COMMENT 'Receiver',
 5   `action` enum('PUBLIC','PRIVATE') NOT NULL DEFAULT 'PUBLIC' COMMENT 'Operation Style',
 6   `chat_content` varchar(255) NOT NULL DEFAULT '' COMMENT 'Chat Log',
 7   `created_at` datetime DEFAULT NULL COMMENT 'Creation Time',
 8   `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update Time',
 9   `deleted_at` timestamp NULL DEFAULT NULL COMMENT 'Delete time',
10   PRIMARY KEY (`id`),
11   KEY `user_id` (`user_id`,`to_user_id`)
12 ) ENGINE=InnoDB AUTO_INCREMENT=116 DEFAULT CHARSET=utf8mb4;

 

friends table

 1 CREATE TABLE `friends` (
 2   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 3   `user_id` int(11) DEFAULT NULL COMMENT 'user id',
 4   `to_user_id` int(11) DEFAULT NULL COMMENT 'Friends id',
 5   `created_at` timestamp NULL DEFAULT NULL,
 6   `updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
 7   `deleted_at` timestamp NULL DEFAULT NULL,
 8   PRIMARY KEY (`id`)
 9 ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
10  

 

notification table

 1 CREATE TABLE `notification` (
 2   `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 3   `type` enum('ADDUSER') NOT NULL DEFAULT 'ADDUSER' COMMENT 'type',
 4   `action` enum('RECEIVE','REFUSE','DEFAULT') DEFAULT 'DEFAULT' COMMENT 'Current category',
 5   `user_id` int(11) NOT NULL COMMENT 'Sender id',
 6   `message` varchar(255) DEFAULT NULL COMMENT 'information',
 7   `to_user_id` int(11) NOT NULL COMMENT 'Receiver id',
 8   `is_read` enum('YES','NO') NOT NULL DEFAULT 'NO' COMMENT 'Is it read',
 9   `created_at` datetime NOT NULL COMMENT 'Creation Time',
10   `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update Time',
11   `deleted_at` timestamp NULL DEFAULT NULL COMMENT 'Delete time',
12   PRIMARY KEY (`id`),
13   KEY `type` (`type`,`user_id`,`to_user_id`)
14 ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb4;

 

users table

 1 CREATE TABLE `users` (
 2   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
 3   `phone` varchar(13) NOT NULL DEFAULT '' COMMENT 'Cell-phone number',
 4   `name` varchar(55) NOT NULL DEFAULT '' COMMENT 'Full name',
 5   `email` varchar(30) NOT NULL DEFAULT '' COMMENT 'E-mail address',
 6   `avatar` varchar(255) DEFAULT NULL COMMENT 'Avatar Address',
 7   `password` varchar(100) NOT NULL DEFAULT '' COMMENT 'Password',
 8   `created_at` datetime DEFAULT NULL COMMENT 'Creation Time',
 9   `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Update Time',
10   `deleted_at` timestamp NULL DEFAULT NULL COMMENT ' Delete time',
11   PRIMARY KEY (`id`),
12   UNIQUE KEY `phone` (`phone`),
13   UNIQUE KEY `email` (`email`)
14 ) ENGINE=InnoDB AUTO_INCREMENT=89 DEFAULT CHARSET=utf8;

 

Posted by magmazing on Sat, 23 Nov 2019 01:45:57 -0800