/jim

A simple instant messaging system developed using Golang. | 使用golang开发的一个简单的即时聊天系统

Primary LanguageGoMIT LicenseMIT

JIM (Jerbe's Instant Messaging): A Simple Chat System

(This code is for learning purposes only and is strictly prohibited for illegal activities)

WARNING: This readme document has been translated from the Chinese version using ChatGPT. If you can read Chinese, please visit this link: [中文版].

Introduction

JIM (Jerbe's Instant Messaging) is a lightweight chat system with the following features

  • Written purely in Golang, supporting private chat, group chat, and world channel chat.
  • Implements some common functionalities, including friend invitation, friend search, friend addition, friend deletion, group creation, group joining, group invitation, and group exit.
  • Easily extensible business logic with relatively clean code and detailed comments.
  • A relatively complete logging system that records various business fields in detail. Logs are in JSON format, suitable for ELK collection and analysis.
  • Supports cross-platform compilation and deployment using cross-compilation.
  • Uses the Swag documentation specification, and you can generate the corresponding API documentation with swag init, making it easy for front-end developers to debug.
  • Beautiful front-end interface
  • Utilizes GitHub Actions for multi-platform compilation, and you can download corresponding platform executables from releases.

Related Components

Language:  golang  
Dependencies:  redis、mongodb、mysql/mariadb
Logging package: github.com/rs/zerolog
SQL operations: github.com/jmoiron/sqlx

Design

1) MySQL is used as the storage database for user information, friend relationships, and group information. MongoDB is used as the message storage database. Redis is used as the subscription service and caching storage service.
2) Users obtain tokens upon login and use tokens to establish WebSocket connections. Since sending messages requires authentication and some policy filtering, WebSocket connections are generally only used to receive server-side messages, and sending chat messages does not depend on WebSocket.
3) In the case of multiple service instances, since the server that a user establishes a WebSocket connection with is random, we use the manager of the `websocket.manager` package in each service instance for unified management. Subscribed data is ultimately distributed to different user connections by the manager.
4) Because we need to manage chat messages, when sending a message, the message is first stored in the database and then subscribed and pushed after being successfully stored.
5) Supports getting the latest X messages from each room and traversing historical messages.

Related Unit Tests

Test Sending Chat Record Speed

  Testing sending speed, 5750 concurrent executions in 10 seconds. Each operation took 2151984 nanoseconds, approximately 2.151984 milliseconds. It doesn't feel fast enough, and there are quite a few allocations. Further optimization is needed. Limited by the machine configuration, it's unclear how it would perform on a high-end configuration.
  
  > /usr/local/opt/go/libexec/bin/go test -c -o /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test github.com/jerbe/jim/handler #gosetup
  > /data/github.com/jerbe/jim/bin/handler_BenchmarkChatSendMessageParallel.test -test.v -test.paniconexit0 -test.bench ^\QBenchmarkChatSendMessageParallel\E$ -test.run ^$ -test.benchmem -test.benchtime=10s
  > goos: darwin
  > goarch: amd64
  > pkg: github.com/jerbe/jim/handler
  > cpu: Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz
  > BenchmarkChatSendMessageParallel-4   	    5750	   2151984 ns/op	  469167 B/op	    3090 allocs/op

Design Diagrams

Basic Architecture Diagram for Message Sending

Message Sending Sequence Diagram

Initialization

1. Deploy the relevant services: MySQL/MariaDB, MongoDB, Redis.
2. Configure the `config.yml` file from the `config` folder with the corresponding service addresses and modify them.
3. Import the SQL statement files from the `sql` folder into MySQL/MariaDB. `all.sql` contains all the database creation statements, while the others contain individual table creation statements.

Features

Here are the planned features:

Accounts

  • Websocket Channel
    • Cross-server push
    • Receive chat messages
    • Receive notification messages
  • User Registration
    • Verification code validation
    • Strong password validation
    • Email binding
    • Limited registration time switch
  • User Login
    • Verification code validation
    • Cannot log in if disabled
    • Cannot log in if deleted
  • User Logout
  • Recover Account
  • Account Information
    • Password modification
    • Avatar modification
    • Nickname modification
    • Online status modification

Friends

  • Find Friends
    • Search by ID
    • Search by nickname
  • Friend Requests
    • Add remarks when requesting friends
    • Greet when becoming friends
  • Friend Editing
    • Modify remarks
    • Remove friends
    • Delete all chat records after removal
    • Block friends
    • Cannot be added as friends again after blocking
  • Friends List
    • Invisible friends not displayed

Groups

  • Create Groups
  • Join Groups
  • Group Member Management
    • Limit the number of members
    • Blacklist group members cannot join again
    • Remove group members
    • Set blacklists when removing group members
    • Appoint group administrators
    • Modify group member nicknames
  • Modify Group Information
    • Transfer group ownership
    • Modify group nickname
    • Modify group avatar
  • Exit Groups
  • Dissolve Groups

Chat

  • Chat List
    • Pin chats
    • Last message in chat room
  • Private Chat
    • Send plain text
    • Send images
    • Send emoji
    • Send videos
    • Send voice
    • Send location
    • Voice chat
    • Video chat
    • Mark messages as read
    • Retract Chat Content
  • Cannot chat if blocked by a friend
    • Cannot chat if deleted by a friend
  • Group Chat
    • Send plain text
    • Send plain text
    • Send images
    • Send emoji
    • Send videos
    • Send voice
    • Send location
    • Voice chat
    • Mark messages as read
    • Retract Chat Content
    • Mute all
    • Mute specific group members
  • World Channel Chat
    • World channel switch
    • World channel mute

Other

[API Documentation][Jcache - (Encapsulated Distributed Cache Integration Solution)]