A simple chat application inside the terminal with Golang and Redis Pub/Sub.
- The chat data is stored as keys.
- For each channel contents, the
channel:is the prefix key, followed by the channel name. - For each message contents, the
message:is the prefix key, followed by thechannel-nameand themessage-datain json format.
- For each channel contents, the
- Here is the sample code to access the channel's messages with Go Redis:
# Get the channel's messages ctx := context.Background() iter := redis.Client.Scan(ctx, 0, fmt.Sprintf("%s%s*", messagePrefix, m.channel), 0).Iterator() var messages []string for iter.Next(ctx) { rawMsg := iter.Val() rawMsg = strings.Replace(rawMsg, fmt.Sprintf("%s%s:", messagePrefix, m.channel), "", 1) message := Message{} _ = json.Unmarshal([]byte(rawMsg), &message) messages = append(messages, formatMessage(message, m.senderStyle)) }
- Go - 1.18
- Clone the repository with
git clonecommand. - Copy the
.env.examplefile to a new.envfile. - Run the
go buildcommand. - Run the
./tchatexecutable file.
To make deploys work, you need to create free account on Redis Cloud(https://redis.info/try-free-dev-to)