Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

A blazingly fast terminal-based chat application demonstrating seamless Foreign Function Interface (FFI) between Go and Swift using C ABI compatibility.

Notifications You must be signed in to change notification settings

jelius-sama/tcat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

37 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš€ tcat - Terminal Chat Application

A blazingly fast terminal-based chat application demonstrating seamless Foreign Function Interface (FFI) between Go and Swift using C ABI compatibility.

๐ŸŽฏ Project Overview

tcat is a test project exploring the powerful combination of Go's concurrency primitives and Swift's expressive syntax through C ABI interoperability. This project showcases:

  • Go Backend: Handles TCP networking, goroutines, channels, and atomic operations
  • Swift Frontend: Implements application logic, terminal UI, and user interactions
  • C ABI Bridge: Enables zero-cost abstractions between both languages

โš ๏ธ Note: This is an experimental/educational project. Authentication is not yet implemented. Do not use in production environments.

โœจ Features

  • ๐ŸŽจ Twitch-Style Colorized Chat: Each user gets a unique, consistent color
  • โšก Real-time Messaging: Powered by Go's goroutines and TCP stack
  • ๐Ÿ–ฅ๏ธ Beautiful TUI: ANSI-based terminal UI with proper scrolling and window resize handling
  • ๐Ÿ’ฌ Blinking Cursor: Visual feedback for input readiness
  • ๐Ÿ”„ Concurrent Architecture: Multiple clients can connect simultaneously
  • ๐ŸŽญ System Notifications: Join/leave messages in subtle gray

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     Swift Application Layer                 โ”‚
โ”‚  โ€ข Terminal UI (ANSI escape codes)                          โ”‚
โ”‚  โ€ข Input handling & message formatting                      โ”‚
โ”‚  โ€ข User interaction logic                                   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ”‚ C ABI FFI
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   Go Runtime Layer (libgolang)              โ”‚
โ”‚  โ€ข TCP networking (Listen, Connect, Accept, Read, Write)    โ”‚
โ”‚  โ€ข Goroutines (TaskLaunch, TaskLaunchVoid)                  โ”‚
โ”‚  โ€ข Channels (ChannelCreate, Send, Recv)                     โ”‚
โ”‚  โ€ข Atomic operations (CompareAndSwap, Load, Store)          โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“‹ Prerequisites

Required Tools

  1. Swift Static SDK (musl-based for Linux)

    export SWIFT_STATIC_SDK=~/.swiftpm/swift-sdks/swift-6.0.3-RELEASE_static-linux-0.0.1.artifactbundle/swift-6.0.3-RELEASE_static-linux-0.0.1/swift-linux-musl/musl-1.2.5.sdk/x86_64
  2. Custom Go Compiler (musl-patched)

    • A patched Go compiler compiled against musl is required
    • Place in bin/musl-go
    • Note: Standard Go has a bug with -buildmode=c-archive when compiled with musl-gcc
  3. Standard Build Tools

    • make
    • gcc or musl-gcc

๐Ÿ”ง Building

# Clone the repository
git clone https://github.com/jelius-sama/tcat.git
cd tcat

# Build everything (Go library + Swift binary)
make

# The binary will be at: bin/tcat

Clean Build

make clean
make

๐Ÿš€ Usage

Starting the Server

./bin/tcat -s -p 6969

Or use the default port (6969):

./bin/tcat -s

Connecting as Client

./bin/tcat -c -p 6969

Multiple clients can connect simultaneously to the same server.

Command Line Options

Usage:
  tcat [mode] [options]

Modes:
  -s, --server          Start in server mode
  -c, --client          Start in client mode
  -h, --help            Show help message

Options:
  -p, --port <port>     Specify port (default: 6969)

Examples:
  tcat -s -p 9000       Start server on port 9000
  tcat -c -p 9000       Connect to server on port 9000
  tcat --help           Show this help

๐ŸŽฎ Controls (Client Mode)

Key Action
Type normally Enter text
Enter Send message
Backspace Delete character
Ctrl+C Exit client

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ bin/
โ”‚   โ”œโ”€โ”€ musl-go              # Patched Go compiler
โ”‚   โ””โ”€โ”€ tcat                 # Compiled binary
โ”œโ”€โ”€ libgolang/
โ”‚   โ”œโ”€โ”€ golang.go            # Go runtime exports
โ”‚   โ”œโ”€โ”€ go.mod
โ”‚   โ”œโ”€โ”€ libgolang.a          # Compiled Go static library
โ”‚   โ”œโ”€โ”€ libgolang.h          # Generated C headers
โ”‚   โ””โ”€โ”€ module.modulemap     # Swift module map
โ”œโ”€โ”€ Source/
โ”‚   โ”œโ”€โ”€ main.swift           # Entry point & CLI parsing
โ”‚   โ”œโ”€โ”€ server.swift         # Server logic
โ”‚   โ”œโ”€โ”€ client.swift         # Client logic & TUI
โ”‚   โ”œโ”€โ”€ shared.swift         # Shared constants
โ”‚   โ”œโ”€โ”€ ctypes.swift         # C type aliases
โ”‚   โ””โ”€โ”€ extension.swift      # Swift extensions
โ””โ”€โ”€ Makefile                 # Build configuration

๐Ÿ”ฌ Technical Deep Dive

FFI Strategy

The project uses C ABI as the common ground between Go and Swift:

  1. Go Side: Functions are exported with //export directive and compiled to a static archive with -buildmode=c-archive
  2. C Headers: Go generates C-compatible headers automatically
  3. Swift Side: Imports the C headers and calls functions directly with zero overhead

Concurrency Model

  • Server: Each client connection runs in its own goroutine via TaskLaunchVoid
  • Client: Two concurrent tasks:
    • Goroutine 1: Reads messages from server
    • Goroutine 2: Blinks cursor for UI feedback
    • Main thread: Handles user input

Synchronization

  • Uses Go's sync/atomic primitives exported to Swift
  • SpinLock implementation with AtomicCompareAndSwapInt32
  • Thread-safe message queue with proper locking

Networking

  • Pure Go net package for TCP operations
  • Connection handles stored in Go-side maps
  • Zero-copy byte slices passed between languages using unsafe.Pointer

๐ŸŽจ UI Design

The terminal UI features:

  • Chat Area: Scrolling message history with color-coded usernames
  • Separator Line: Visual boundary between chat and input
  • Input Line: Shows username + current input + blinking cursor
  • Dynamic Resizing: Adapts to terminal window size changes (SIGWINCH)

Colors are assigned deterministically based on username hash, ensuring consistency across reconnects.

๐Ÿ› Known Limitations

  • โŒ No authentication or encryption
  • โŒ No message persistence
  • โŒ No private messaging
  • โŒ No rate limiting or spam protection

๐Ÿ”ฎ Future Improvements

  • Add TLS/SSL encryption
  • Implement user authentication
  • Add private messaging support
  • Message history/logging
  • Configurable color themes
  • Emoji support
  • File transfer capability
  • Multiple chat rooms

๐Ÿค Contributing

This is an experimental project for learning purposes. Feel free to fork and experiment!

๐Ÿ“„ License

MIT โ€” See LICENSE

โœจ Author

Jelius Basumatary โ€” Systems & App Developer in Practice

๐Ÿ™ Acknowledgments

  • Go team for excellent cgo and concurrency primitives
  • Terminal emulator developers for ANSI standard support

Built with โค๏ธ using Go and Swift

About

A blazingly fast terminal-based chat application demonstrating seamless Foreign Function Interface (FFI) between Go and Swift using C ABI compatibility.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published