A lightweight peer-to-peer chat application built with Go and WebRTC that allows users to communicate directly over the internet without relying on centralized messaging servers.
- Real-time Messaging: Direct peer-to-peer text communication using WebRTC data channels
- NAT Traversal: Automatic connection establishment through firewalls and NAT using STUN servers
- Minimal Interface: Clean, responsive design focused on functionality
- No Registration: No accounts or registration required - just share your peer ID
- Cross-Platform: Works in any modern web browser on desktop and mobile devices
- WebSocket-based signaling server for coordinating peer connections
- Handles SDP offer/answer exchange and ICE candidate relay
- Maintains registry of connected peers
- Serves static frontend files
- Responsive web interface with modern design
- WebRTC peer connection management
- Real-time message display with chat bubbles
- Connection status indicators and user feedback
- Go 1.21 or later
- Modern web browser with WebRTC support
-
Clone or download the project files
-
Install dependencies:
cd p2p-chat go mod tidy -
Start the server:
go run main.go
-
Open your browser and navigate to
http://localhost:8080
- Get your Peer ID: When you open the application, you'll see your unique peer ID in the "Your ID" field
- Share your ID: Copy your peer ID and share it with the person you want to chat with
- Connect to a peer: Enter their peer ID in the "Connect to Peer" field and click "Connect"
- Start chatting: Once connected, type messages in the input area and press Enter or click Send
For testing on a local network, simply run the server and access it via the server's IP address:
go run main.go
# Access via http://[server-ip]:8080For internet-wide access, deploy the server on a public cloud instance:
- Deploy to a cloud server (AWS, Google Cloud, DigitalOcean, etc.)
- Configure firewall to allow traffic on port 8080
- Update STUN/TURN configuration if needed for better NAT traversal
- Use HTTPS for production deployment (recommended)
# Build the application
go build -o p2p-chat main.go
# Run on a specific port
./p2p-chat
# Or with environment variables
PORT=8080 ./p2p-chatCreate a Dockerfile:
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go mod tidy && go build -o p2p-chat main.go
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/p2p-chat .
COPY --from=builder /app/static ./static
COPY --from=builder /app/assets ./assets
EXPOSE 8080
CMD ["./p2p-chat"]Build and run:
docker build -t p2p-chat .
docker run -p 8080:8080 p2p-chatThe application uses Google's public STUN servers by default. For production use, consider:
- Using your own STUN server for better reliability
- Adding TURN servers for users behind restrictive firewalls
- Configuring multiple STUN/TURN servers for redundancy
Update the rtcConfig in static/webrtc.js:
this.rtcConfig = {
iceServers: [
{ urls: 'stun:stun.l.google.com:19302' },
{ urls: 'stun:your-stun-server.com:19302' },
{
urls: 'turn:your-turn-server.com:3478',
username: 'your-username',
credential: 'your-password'
}
]
};For production deployment:
- Use HTTPS: WebRTC requires secure contexts for many features
- Implement rate limiting: Prevent abuse of the signaling server
- Add authentication: Consider adding user authentication if needed
- Monitor connections: Log and monitor peer connections for debugging
- Content filtering: Implement message filtering if required
-
Connection fails:
- Check if both users are online
- Verify peer IDs are correct
- Ensure STUN servers are accessible
-
Messages not sending:
- Verify WebRTC connection is established
- Check browser console for errors
- Ensure data channel is open
-
Can't connect through firewall:
- Configure TURN servers for relay
- Check corporate firewall settings
- Try different STUN servers
- Chrome 56+
- Firefox 51+
- Safari 11+
- Edge 79+
- WebSocket support (port 8080 by default)
- UDP traffic for WebRTC (various ports)
- Access to STUN servers (port 19302)
p2p-chat/
├── main.go # Go signaling server
├── go.mod # Go dependencies
├── static/ # Frontend files
│ ├── index.html # Main HTML page
│ ├── styles.css # CSS styling
│ ├── webrtc.js # WebRTC management
│ └── app.js # Application logic
├── assets/ # Visual assets
└── README.md # This file
The application is designed to be easily extensible:
- File sharing: Extend data channels to support file transfer
- Voice/video: Add media streams to peer connections
- Group chat: Implement multi-peer connections
- Message history: Add local storage for chat history
- Encryption: Implement end-to-end encryption for messages
This project is provided as-is for educational and demonstration purposes. Feel free to modify and use according to your needs.
For issues and questions:
- Check the troubleshooting section above
- Review browser console for error messages
- Verify network connectivity and firewall settings
- Test with different browsers or devices