{"id":19165113,"url":"https://github.com/tookit/gochat-server","last_synced_at":"2026-02-26T20:47:17.281Z","repository":{"id":81543200,"uuid":"343251193","full_name":"tookit/gochat-server","owner":"tookit","description":null,"archived":false,"fork":false,"pushed_at":"2020-07-01T06:11:53.000Z","size":8835,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T23:14:25.251Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tookit.png","metadata":{"files":{"readme":"readme.en.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-01T00:55:03.000Z","updated_at":"2022-04-12T02:55:50.000Z","dependencies_parsed_at":"2023-03-02T07:01:00.735Z","dependency_job_id":null,"html_url":"https://github.com/tookit/gochat-server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tookit/gochat-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tookit%2Fgochat-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tookit%2Fgochat-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tookit%2Fgochat-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tookit%2Fgochat-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tookit","download_url":"https://codeload.github.com/tookit/gochat-server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tookit%2Fgochat-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29872659,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-26T18:42:30.764Z","status":"ssl_error","status_checked_at":"2026-02-26T18:41:47.936Z","response_time":89,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T09:26:33.759Z","updated_at":"2026-02-26T20:47:17.250Z","avatar_url":"https://github.com/tookit.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🚀 gochat \n[![Build Status](https://travis-ci.org/LockGit/gochat.svg?branch=master)](https://travis-ci.org/LockGit/gochat)\n\u003cimg src=\"https://img.shields.io/badge/gochat-im-green\"\u003e\n\u003cimg src=\"https://img.shields.io/badge/documentation-yes-brightgreen.svg\"\u003e\n\u003cimg src=\"https://img.shields.io/github/license/LockGit/gochat\"\u003e\n[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/LockGit/gochat/issues)\n[![HitCount](http://hits.dwyl.io/LockGit/gochat.svg)](http://hits.dwyl.io/LockGit/gochat)\n\u003cimg src=\"https://img.shields.io/github/contributors/LockGit/gochat\"\u003e\n\u003cimg src=\"https://img.shields.io/github/last-commit/LockGit/gochat\"\u003e\n\u003cimg src=\"https://img.shields.io/github/issues/LockGit/gochat\"\u003e \n\u003cimg src=\"https://img.shields.io/github/forks/LockGit/gochat\"\u003e \n\u003cimg src=\"https://img.shields.io/github/stars/LockGit/gochat\"\u003e\n\u003cimg src=\"https://img.shields.io/docker/pulls/lockgit/gochat\"\u003e\n\u003cimg src=\"https://img.shields.io/github/repo-size/LockGit/gochat\"\u003e\n\u003cimg src=\"https://img.shields.io/github/followers/LockGit\"\u003e\n\n### [中文版本(Chinese version)](readme.md)\n\n### Gochat is a lightweight im server implemented using pure go\n```\ngochat is an instant messaging system implemented by pure go. \nIt supports private message and room broadcast messages. \nrpc communication is provided between layers to support horizontal expansion.\nUsing redis as a carrier for message storage and delivery, \nit is more convenient and faster to operate than kafka.\nso it is very lightweight. \nBased on the etcd service discovery between layers, \nit will be much more convenient when expanding and deploying.\nDue to the cross-compilation feature of go, \nit can be run on various platforms quickly after compilation. \nThe gochat architecture and directory structure are clear.\nAnd the project also provides docker one-click to build all environment dependencies, \nwhich is very convenient to install.\n```\n\n### Architecture design\n ![](https://github.com/LockGit/gochat/blob/master/architecture/gochat.png)\n\n### Service discovery\n![](https://github.com/LockGit/gochat/blob/master/architecture/gochat_discovery.png)\n\n\n### Message delivery\n![](https://github.com/LockGit/gochat/blob/master/architecture/single_send.png)\n```\nThe message must be sent in the login state. As shown above, User A sends a message to User B. \nThen experienced the following process:\n1. User A invokes the api layer interface to log in to the system. After the login succeeds, \nthe user keeps a long link with the connect layer authentication, \nand the rpc call logic layer records the serverId that user A logs in at the connect layer. \nThe default joins the room number 1.\n\n2. User B calls the api layer interface to log in to the system. \nAfter the login succeeds, the user keeps a long link with the connect layer authentication, \nand the rpc call logic layer records the serverId of the user B login at the connect layer. \nThe default joins the room number 1.\n\n3. User A calls the api layer interface to send a message, \nand the api layer rpc call logic layer sends a message method. \nThe logic layer pushes the message to the queue and waits for the task layer to consume.\n\n4, After the task layer subscribes to the message sent by the logic layer to the queue, \naccording to the message content (userId, roomId, serverId), \nthe user B can be positioned to maintain a long link on the serverId of the connect layer, \nand the rpc call connect layer method is further\n\n5, After the connect layer is rpc call by the task layer, \nthe message will be delivered to the relevant room, \nand then further delivered to the user B in the room to complete a complete message session communication.\n\n6, if it is a private message, \nthen the task layer will locate the serverId corresponding to the connect layer according to the userId, \ndirectly rpc call the connect layer of the serverId, and complete the private message delivery.\n\nLearning other im systems, in order to reduce the lock competition, will be divided in the connect layer bucket:\nIt will roughly look like the following structure:\nConnect layer:\n     Bucket: (bucket, reduce lock competition)\n         Room: (room)\n             Channel: (user session)\n```\n\n### Chat room preview\n![](https://github.com/LockGit/gochat/blob/master/architecture/gochat.gif)\n\n\n### Directory Structure\n```\n➜ gochat git:(master) ✗ tree -L 1\n.\n├── api             # api interface layer, providing rest api service, main rpc call logic layer service, horizontal expansion\n├──architecture     # architecture resource picture folder\n├── bin             # golang compiled binary, do not submit to the git repository\n├── config          # configuration file\n├── connect         # link layer, this layer is used for handlers to live a large number of user long connections, real-time message push, in addition, the main rpc call logic layer service, can be horizontally extended\n├── db              # database link initialization, internal gochat.sqlite3 for the convenience of the use of the sqlite database, the actual scene can be replaced with other relational database\n├── docker          # for quickly building a docker environment\n├── go.mod          # go package management mod file\n├── go.sum          # go package management sum file\n├── logic           # Logic layer, this layer mainly receives the rpc request of the connect layer and the api layer. If it is a message, it will be pushed to the queue, and finally consumed by the task layer, which can be horizontally expanded.\n├── main.go         # gochat program only entry file\n├── proto           # golang rpc proto file\n├── readme.md       # Chinese documentation\n├── readme.en.md    # English documentation\n├── reload.sh       # Compile gochat and execute supervisorctl restart to restart related processes\n├── run.sh          # Quickly build a docker container and start the demo\n├── site            # site layer, this layer is a pure static page, will http request api layer, can be extended horizontally\n├── task            # task layer, the data in the consumption queue of this layer, and the rpc call connect layer performs message transmission, which can be extended horizontally\n├── tools           # tools function\n└── vendor          # vendor package\n```\n\n### Related components\n```\nLanguage: golang\n\nDatabase: sqlite3\ncan be replaced by mysql or other database according to the actual business scenario. In this project, \nfor the convenience of demonstration, use sqlite instead of large relational database, \nonly store simple user information\n\nDatabase ORM: gorm\n\nService discovery: etcd\n\nRpc communication: rpcx\n\nQueue: redis \nconvenient to use redis, can be replaced with kafka or rabbitmq according to the actual situation\n\nCache: redis \nstore user session, and related counters, chat room information, etc.\n\nMessage id: \nThe snowflakeId algorithm, \nwhich can be split into microservices separately, \nmaking it part of the underlying service. \nThe id numbering device qps theory is up to 409.6w/s. \nNo company on the Internet can achieve such high concurrency unless it suffers from DDOS attacks.\n```\n\n### Database and table structure\n```\nIn this demo, in order to be lightweight and convenient to demonstrate, \nthe database uses sqlite3, based on gorm, \nso this can be replaced. \nIf you need to replace other relational databases, just modify the relevant db driver.\n\nRelated table structure:\ncd db \u0026\u0026 sqlite3 gochat.sqlite3\n.tables\n```\n```sqlite\ncreate table user(\n  `id` INTEGER PRIMARY KEY autoincrement , -- 'userId'\n  `user_name` varchar(20) not null UNIQUE default '', -- 'userName'\n  `password` char(40) not null default '', -- 'passWord'\n  `create_time` timestamp NOT NULL DEFAULT current_timestamp -- 'createTime'\n);\n```\n\n### Installation\n```\nBefore starting each layer, \nPlease make sure that the 7000, 7070, 8080 ports are not occupied.\nmake sure that the etcd and redis services and the above database tables have been started, \nand then start the layers in the following order. \n\nIf you want to expand the connect layer, \nmake sure that the serverId in the connect layer configuration is different!\n\n0,Compile\ngo build -o gochat.bin -tags=etcd main.go\n\n1,Start the logic layer\n./gochat.bin -module logic\n\n2,Start the connect layer\n./gochat.bin -module connect\n\n3,Start the task layer\n./gochat.bin -module task\n\n4,Start the api layer\n./gochat.bin -module api \n\n5,Start a site and start a chat room\n./gochat.bin -module site\n```\n\n### Start with a docker\n```\nIf you feel that the above steps are too cumbersome, \nyou can use the following docker image to build all the dependencies and quickly start a chat room.\n\nYou can use the image I pushed to the docker hub \nthere have been several test users created in the default image.\nusername  password\ndemo        111111\ntest        111111\nadmin       111111\n1,docker pull lockgit/gochat:latest\n2,git clone git@github.com:LockGit/gochat.git\n3,cd gochat \u0026\u0026 sh run.sh dev (This step requires a certain time to compile each module and wait patiently. Some systems may not have sh. if an error is reported during execution, change sh run.sh dev to ./ run.sh dev for execution)\n4,visit http://127.0.0.1:8080 to open the chat room\n\n\nIf you want to build an image yourself, \nyou only need to build the Dockerfile under the docker file.\nDocker build -f docker/Dockerfile . -t lockgit/gochat\nthen execute:\n1,git clone git@github.com:LockGit/gochat.git\n2,cd gochat \u0026\u0026 sh run.sh dev (This step requires a certain time to compile each module and wait patiently. Some systems may not have sh. if an error is reported during execution, change sh run.sh dev to ./ run.sh dev for execution)\n3,visit http://127.0.0.1:8080 to open the chat room\n\nIf you want to deploy on personal vps, \nremember to change the address of socketUrl and apiUrl in site/js/common.js to your ip address of vps.\nAnd make sure there are no firewall restrictions on the relevant ports on the vps.\n```\n\n### Here is an online chat demo site:\n\u003ca href=\"http://www.lockhack.cf:8080\" target=\"_blank\"\u003ehttp://www.lockhack.cf:8080\u003c/a\u003e\n```\nLog in with the above user name and password, or register one by yourself.\nif the provide url can't visit one day，please use the docker above to visit it.\nYou can log in to different browsers with different accounts. \nIf it is Chrome, you can use several browsers in incognito mode to log in with different accounts.\nThen, experience the chat between different accounts and the effect of message delivery.\n```\n\n### Follow-up\n```\ngochat implements a simple chat room function. Due to limited energy, \nyou can use your own business logic to customize some requirements and optimize some code in gochat.\nPlease contact the relevant issue for any questions in use, \nand follow up and optimize the relevant code according to the actual situation.\n```\n\n### Discuss\n```\nBecause some students mentioned the issue proposal to establish a communication group, \nthe following two QR codes are provided for technical learning exchange and recording related technical experience, \nand advertising is prohibited.\n```\n\u003eQQ Group\n\n![QQ](https://github.com/LockGit/gochat/blob/master/architecture/gochat-qq.jpg)\n\n\u003eWechat (Share something interesting from time to time)\n\n![Wechat](https://github.com/LockGit/gochat/blob/master/architecture/gochat-wx.jpg)\n\n### Backer and Sponsor\n\u003ejetbrains\n\n\u003ca href=\"https://www.jetbrains.com/?from=LockGit/gochat\" target=\"_blank\"\u003e\n\u003cimg src=\"https://github.com/LockGit/gochat/blob/master/architecture/jetbrains-gold-reseller.svg\" width=\"100px\" height=\"100px\"\u003e\n\u003c/a\u003e\n\n### License\ngochat is licensed under the MIT License. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftookit%2Fgochat-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftookit%2Fgochat-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftookit%2Fgochat-server/lists"}