This talk provides a hands-on guide to building a web server in Elixir, starting from fundamental internet concepts and progressing to a functional Elixir implementation. The speaker, Bert, first demystifies the Internet's structure (computers, names, ports, IP addresses) and the mechanics of web communication, including DNS resolution and the HTTP request-response cycle, using tools like Curl and Telnet for demonstration. The core of the presentation then transitions to Elixir, showcasing its strengths as a functional language running on the Erlang VM for concurrent, fault-tolerant web services. Bert walks through the iterative development of a simple web server, demonstrating how to handle incoming requests, construct valid HTTP responses with headers (status codes, content types), perform rudimentary routing based on request paths, and leverage Elixir's string interpolation and pipe operator for clean code. The talk concludes with a Q&A session covering the speaker's development environment and Elixir's approach to dynamic typing, exhaustiveness checking, and practical error prevention.
Building a Web Server in Elixir: A Functional Approach
0:00:22 - Talk Introduction & Functional Programming: Bert introduces the talk on building an Elixir web server, emphasizing a functional programming paradigm for handling requests and responses.
0:01:45 - Internet Fundamentals (Computers, Names, Ports, IP Addresses): Explanation of basic internet concepts, including computer hostnames, standard ports (80 for HTTP, 443 for HTTPS, 53 for DNS), and IP addresses (IPv4 and IPv6).
0:04:24 - Web Communication (DNS & HTTP Flow): Details on how clients resolve domain names to IP addresses via DNS and initiate HTTP GET requests, receiving responses with status codes like 200 OK.
0:06:58 - Curl Demonstration:curl example.com is used to show a basic HTML response, and curl -v example.com demonstrates the full HTTP request and response headers, including the DNS resolution phase.
0:09:32 - Manual HTTP Request with Telnet: A hands-on demo of manually crafting and sending an HTTP GET request using Telnet, illustrating the raw protocol interaction.
0:11:09 - Elixir Introduction & Key Features: An overview of Elixir as a functional language on the Erlang VM, highlighting its concurrency model (lightweight processes) and core language features like string manipulation, lists, and the pipe operator (|>).
0:14:48 - Elixir Control Structures: Introduction to case statements for pattern matching and def/do end for defining functions, crucial for building the web server logic.
0:18:11 - Underlying TCP/IP Layer: Explanation of how the web server utilizes the TCP layer (via Erlang's gen_tcp) to listen for connections, accept them in an infinite loop, and spawn a new, lightweight Elixir process for each incoming client connection.
0:21:25 - Initial Elixir Web Server Implementation: The first attempt at writing a response_tdf function, which initially fails to return a valid HTTP response due to missing protocol headers.
0:23:00 - Adding HTTP Headers for Valid Responses: Implementation of HTTP 1.1 status (200 OK), Content-Type, and Content-Length to ensure proper communication with the client.
0:24:53 - Dynamic Response Construction: Demonstrating Elixir's string interpolation and the byte_size function to dynamically calculate and insert the Content-Length into the response header.
0:27:50 - Serving HTML Content: Modifying the Content-Type to text/html and providing an HTML string as the response body.
0:28:22 - Basic Routing & Request Parsing: Showing how to parse the incoming request string to extract the path (e.g., / or /TDF) and use Elixir's case statements for rudimentary URL-based routing. The importance of a catch-all case is emphasized for graceful error handling.
0:33:00 - Extending Server Functionality: Discussion on how the server could be expanded to handle different HTTP methods (POST, GET), load files from the file system, or process query parameters by further pattern matching on the request.
0:35:49 - Q&A: Editor & Elixir Typing: Questions regarding the speaker's Emacs setup, Language Server Protocol (LSP) for type annotations, and Elixir's dynamic typing system, including tools like Dialyzer and the upcoming gradual typing system.
0:43:04 - Q&A: Practical Dynamic Typing Experience: Bert shares his experience, noting that Elixir's design (concurrency, small functions, pipe operator) helps prevent errors, leading to fewer issues in practice despite dynamic typing.