Network (net)
The net module in Flame provides a full-featured, asynchronous networking toolkit. It is modular, meaning you can import only the specific networking components you need, minimizing compilation overhead for embedded or lightweight projects.
Core Networking
Section titled “Core Networking”TCP Sockets (std.net.tcp)
Section titled “TCP Sockets (std.net.tcp)”The tcp module provides TcpListener and TcpSocket for raw socket communication.
import std.net.tcp
@Application(features=["tcp"])async fn start() { let listener = tcp.TcpListener.bind("0.0.0.0:3000") for client in listener { await client.write("Hello from Flame!") }
let socket = tcp.TcpSocket.connect("example.com:80") socket.write("GET / HTTP/1.1\r\n\r\n") let data = socket.read()}UDP Sockets (std.net.udp)
Section titled “UDP Sockets (std.net.udp)”The udp module provides connectionless datagram sockets.
import std.net.udp
@Application(features=["udp"])async fn start() { let udp = udp.UdpSocket.bind(":9000") udp.send("Hello", "192.168.0.10:9000")
let (msg, addr) = udp.recv()}Application Protocols
Section titled “Application Protocols”HTTP Client (std.net.http)
Section titled “HTTP Client (std.net.http)”The http module provides a powerful asynchronous HTTP client supporting GET, POST, PUT, DELETE, PATCH, downloading, and uploading.
When you make an HTTP request, Flame returns a special Response Object. If you try to print this response directly (println(res)), it will display its internal structure: <object [text, json, status, ok]>.
To access the actual data, you must call its methods or properties:
res.status: The integer HTTP status code (e.g.,200,404).res.ok: A boolean indicating if the status is successful (200-299).res.text(): A closure returning the raw string body of the response.res.json(): A closure returning the parsed JSON formula of the response.
import std.net.http
@Application(features=["http"])async fn app() { let response = await http.get("https://api.github.com/users/shoya-129")
println(response.status) // Prints: 200 println(response.ok) // Prints: true
// You must call the .text() or .json() functions to extract the body let raw_text = response.text() println(raw_text)
// Parsing JSON directly from GET let response2 = await http.get("https://api.github.com") let user = response2.json()
// Making a POST request with an auto-stringified JSON payload let postData = formula { title: "foo", body: "bar", userId: 1 } let post_res = await http.post("https://httpbin.org/post", postData) println(post_res.text())}WebSockets (std.net.ws)
Section titled “WebSockets (std.net.ws)”The ws module provides a WebSocket client for real-time bi-directional communication.
import std.net.ws
@Application(features=["ws"])async fn start() { let ws = await ws.WebSocket.connect("ws://localhost:8080/ws") await ws.send("Move Forward") let msg = await ws.recv()}MQTT Client (std.net.mqtt)
Section titled “MQTT Client (std.net.mqtt)”The mqtt module provides an MQTT client, useful for IoT and automation.
import std.net.mqtt
@Application(features=["mqtt"])async fn start() { let client = await mqtt.Mqtt.connect("mqtt://broker.local") await client.publish("robot/move", "forward") await client.subscribe("sensor/temp") |msg| { println(msg) }}Utilities
Section titled “Utilities”DNS (std.net.dns)
Section titled “DNS (std.net.dns)”import std.net.dns
@Application(features=["dns"])async fn start() { let ip = await dns.lookup("google.com")}Network Interfaces (std.net.interface)
Section titled “Network Interfaces (std.net.interface)”import std.net.interface
@Application(features=["net"])async fn start() { for iface in await interface.interfaces() { println(iface.name) }}URL Parsing (std.net.url)
Section titled “URL Parsing (std.net.url)”import std.net.url
let u = await url.Url.parse("https://github.com/shoya-129/flame?branch=main")println(u.host)println(u.query)