A simple real-time chat application built with Go and PubNub that allows multiple clients to exchange messages instantly.
- ✅ Real-time messaging between multiple clients
- ✅ User presence notifications (join/leave events)
- ✅ Simple command-line interface
- ✅ Message timestamps
- ✅ Built entirely with Go and PubNub SDK
- Go 1.21 or later
- PubNub account and Publish / Subscribe keys
git clone https://github.com/PubNubDevelopers/pubnub-go-chat.git
cd pubnub-go-chat
- Sign up for a free PubNub account at https://admin.pubnub.com
- Create a new app in the PubNub Admin Portal
- Enable Presence Events on the keyset
- Copy the Publish and Subscribe keys from the app's keyset
Edit the constants in main.go
to define your PubNub keys. You don't need to change the CHAT_CHANNEL value.
const (
PUBLISH_KEY = "Change me: your-publish-key-here"
SUBSCRIBE_KEY = "Change me: your-subscribe-key-here"
CHAT_CHANNEL = "chat-room"
)
go mod tidy
To test the chat functionality between two clients, open two terminal windows:
Terminal 1:
go run main.go
Terminal 2:
go run main.go
Enter different usernames in each terminal and start chatting!
- When you start the application, you'll be prompted to enter a username
- Once connected, you can start typing messages
- Messages from other users will appear in real-time
- You'll see presence notifications when users join or leave the chat. This is powered by PubNub presence
- Type
quit
orexit
to leave the chat
🚀 PubNub Go Chat Application
=============================
Enter your username: Alice
Welcome Alice! You're now entering the chat room.
Type 'quit' to exit the chat.
=============================
✅ Connected to PubNub! You can start chatting now.
🟢 Bob joined the chat
You: Hello everyone!
💬 [14:32:15] Bob: Hi Alice! How are you?
You: I'm doing great! How about you?
💬 [14:32:30] Bob: Fantastic! This chat app works perfectly.
You: quit
👋 Goodbye!
The application uses the following PubNub features:
- Publish/Subscribe: Core messaging functionality
- Presence: Tracks when users join/leave the chat
- Real-time Events: Handles incoming messages and status updates through PubNub channels
- PubNub Configuration: Sets up the client with your API keys
- Event Listener: Handles incoming messages, presence events, and connection status
- Message Publishing: Sends chat messages to the PubNub channel
- Presence Monitoring: Shows when users join or leave the chat room
You can customize the application by:
- Changing the
CHAT_CHANNEL
constant to use different chat rooms - Modifying the
ChatMessage
struct to include additional fields - Adding command parsing for special chat commands (similar to how the current app handles
quit
andexit
) - Add support for message history
- Verify your internet connection
- Check that your PubNub keys are correct
- Ensure you're not behind a firewall blocking PubNub connections
- Check you have presence enabled on your keyset
- Check presence is enabled on your channel (see presence management in the portal)
- Make sure both clients are subscribed to the same channel
- Check for any error messages in the console
- Verify the publish operation is successful
- Ensure Go version 1.21 or later is installed
- Run
go mod tidy
to resolve dependencies - Check that all imports are correct
Contact the PubNub devrel team at devrel@pubnub.com if you have any other questions, issues or comments.
This project is open source and available under the MIT License.