Skip to content

Latest commit

 

History

History
218 lines (162 loc) · 6.04 KB

File metadata and controls

218 lines (162 loc) · 6.04 KB

P2P Chat Application

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.

Features

  • 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

Architecture

Backend (Go Signaling Server)

  • 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

Frontend (HTML/CSS/JavaScript)

  • Responsive web interface with modern design
  • WebRTC peer connection management
  • Real-time message display with chat bubbles
  • Connection status indicators and user feedback

Quick Start

Prerequisites

  • Go 1.21 or later
  • Modern web browser with WebRTC support

Installation & Running

  1. Clone or download the project files

  2. Install dependencies:

    cd p2p-chat
    go mod tidy
  3. Start the server:

    go run main.go
  4. Open your browser and navigate to http://localhost:8080

Usage

  1. Get your Peer ID: When you open the application, you'll see your unique peer ID in the "Your ID" field
  2. Share your ID: Copy your peer ID and share it with the person you want to chat with
  3. Connect to a peer: Enter their peer ID in the "Connect to Peer" field and click "Connect"
  4. Start chatting: Once connected, type messages in the input area and press Enter or click Send

Deployment

Local Network Deployment

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]:8080

Public Internet Deployment

For internet-wide access, deploy the server on a public cloud instance:

  1. Deploy to a cloud server (AWS, Google Cloud, DigitalOcean, etc.)
  2. Configure firewall to allow traffic on port 8080
  3. Update STUN/TURN configuration if needed for better NAT traversal
  4. Use HTTPS for production deployment (recommended)

Example deployment commands:

# Build the application
go build -o p2p-chat main.go

# Run on a specific port
./p2p-chat

# Or with environment variables
PORT=8080 ./p2p-chat

Docker Deployment (Optional)

Create 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-chat

Configuration

STUN/TURN Servers

The application uses Google's public STUN servers by default. For production use, consider:

  1. Using your own STUN server for better reliability
  2. Adding TURN servers for users behind restrictive firewalls
  3. 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'
        }
    ]
};

Security Considerations

For production deployment:

  1. Use HTTPS: WebRTC requires secure contexts for many features
  2. Implement rate limiting: Prevent abuse of the signaling server
  3. Add authentication: Consider adding user authentication if needed
  4. Monitor connections: Log and monitor peer connections for debugging
  5. Content filtering: Implement message filtering if required

Troubleshooting

Common Issues

  1. Connection fails:

    • Check if both users are online
    • Verify peer IDs are correct
    • Ensure STUN servers are accessible
  2. Messages not sending:

    • Verify WebRTC connection is established
    • Check browser console for errors
    • Ensure data channel is open
  3. Can't connect through firewall:

    • Configure TURN servers for relay
    • Check corporate firewall settings
    • Try different STUN servers

Browser Compatibility

  • Chrome 56+
  • Firefox 51+
  • Safari 11+
  • Edge 79+

Network Requirements

  • WebSocket support (port 8080 by default)
  • UDP traffic for WebRTC (various ports)
  • Access to STUN servers (port 19302)

Development

Project Structure

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

Adding Features

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

License

This project is provided as-is for educational and demonstration purposes. Feel free to modify and use according to your needs.

Support

For issues and questions:

  1. Check the troubleshooting section above
  2. Review browser console for error messages
  3. Verify network connectivity and firewall settings
  4. Test with different browsers or devices