Top Related Projects
Peace of mind from prototype to production
Fast, unopinionated, minimalist web framework for node.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
High performance, minimalist Go web framework
⚡️ Express inspired web framework written in Go
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
Quick Overview
Cowboy is a small, fast, and modern HTTP server for Erlang/OTP. It is designed to be lightweight and efficient, making it suitable for building scalable web applications and RESTful APIs. Cowboy supports both HTTP/1.1 and HTTP/2 protocols.
Pros
- High performance and low latency
- Modular and extensible architecture
- Excellent WebSocket support
- Comprehensive documentation and examples
Cons
- Steeper learning curve for developers new to Erlang
- Limited ecosystem compared to more popular web frameworks
- May require additional libraries for complex web applications
- Not as feature-rich out-of-the-box as some full-stack frameworks
Code Examples
- Basic HTTP handler:
-module(hello_handler).
-behavior(cowboy_handler).
-export([init/2]).
init(Req0, State) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello, World!">>, Req0),
{ok, Req, State}.
This example defines a simple HTTP handler that responds with "Hello, World!".
- WebSocket handler:
-module(ws_handler).
-behavior(cowboy_websocket).
-export([init/2, websocket_handle/2, websocket_info/2]).
init(Req, State) ->
{cowboy_websocket, Req, State}.
websocket_handle({text, Msg}, State) ->
{reply, {text, <<"You said: ", Msg/binary>>}, State};
websocket_handle(_Data, State) ->
{ok, State}.
websocket_info(_Info, State) ->
{ok, State}.
This example implements a WebSocket handler that echoes back messages prefixed with "You said: ".
- Routing configuration:
Dispatch = cowboy_router:compile([
{'_', [
{"/", hello_handler, []},
{"/ws", ws_handler, []}
]}
]),
{ok, _} = cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
).
This code sets up routing for the HTTP and WebSocket handlers defined above.
Getting Started
To use Cowboy in your Erlang project:
- Add Cowboy as a dependency in your
rebar.config
:
{deps, [
{cowboy, "2.9.0"}
]}.
- Start the Cowboy application in your
app.src
file:
{applications, [
kernel,
stdlib,
cowboy
]}.
-
Implement your handlers and set up routing as shown in the code examples above.
-
Compile and run your application:
$ rebar3 compile
$ rebar3 shell
Your Cowboy server should now be running and accessible at http://localhost:8080
.
Competitor Comparisons
Peace of mind from prototype to production
Pros of Phoenix
- Full-featured web framework with built-in ORM (Ecto), routing, and templating
- Designed for high productivity and real-time applications
- Strong community support and extensive documentation
Cons of Phoenix
- Steeper learning curve due to more complex architecture
- Heavier resource usage, potentially slower for simple applications
- Opinionated structure may be limiting for some projects
Code Comparison
Phoenix (Router):
defmodule MyApp.Router do
use Phoenix.Router
get "/", PageController, :index
resources "/users", UserController
end
Cowboy (Handler):
init(Req0, State) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello World!">>, Req0),
{ok, Req, State}.
Summary
Phoenix is a full-stack web framework built on top of Cowboy, offering more features and abstractions. It's ideal for complex, real-time applications but may be overkill for simple projects. Cowboy is a lightweight, low-level HTTP server that provides more flexibility but requires more manual setup. Choose Phoenix for rapid development of feature-rich web applications, and Cowboy for fine-grained control over HTTP handling in simpler or performance-critical scenarios.
Fast, unopinionated, minimalist web framework for node.
Pros of Express
- Larger ecosystem and community support
- More extensive middleware options
- Easier to learn and use for JavaScript developers
Cons of Express
- Generally slower performance compared to Cowboy
- Less suitable for real-time applications and WebSockets
- Higher memory usage, especially under heavy loads
Code Comparison
Express (JavaScript):
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
Cowboy (Erlang):
-module(hello_handler).
-export([init/2]).
init(Req0, State) ->
Req = cowboy_req:reply(200, #{<<"content-type">> => <<"text/plain">>}, <<"Hello World!">>, Req0),
{ok, Req, State}.
Summary
Express is a popular web application framework for Node.js, known for its simplicity and extensive ecosystem. It's ideal for quickly building web applications and APIs using JavaScript. Cowboy, on the other hand, is a small, fast, and modular HTTP server for Erlang/OTP. It excels in performance and scalability, making it suitable for high-concurrency scenarios and real-time applications. While Express offers easier development for JavaScript developers, Cowboy provides better performance and resource utilization, especially for large-scale systems.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Pros of Gin
- Written in Go, offering better performance and concurrency handling
- Minimalist design with a focus on simplicity and ease of use
- Extensive middleware support and built-in routing capabilities
Cons of Gin
- Less mature ecosystem compared to Cowboy's Erlang/OTP foundation
- Limited built-in features, often requiring third-party packages for advanced functionality
- Smaller community and fewer long-term production deployments
Code Comparison
Gin (Go):
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{"message": "pong"})
})
r.Run()
Cowboy (Erlang):
Dispatch = cowboy_router:compile([
{'_', [{"/ping", ping_handler, []}]}
]),
{ok, _} = cowboy:start_clear(http, [{port, 8080}], #{
env => #{dispatch => Dispatch}
}).
Both frameworks offer simple routing and request handling, but Gin's syntax is more concise and Go-idiomatic. Cowboy's approach is more aligned with Erlang's pattern matching and OTP principles. Gin excels in rapid development and ease of use, while Cowboy leverages Erlang's robustness and scalability for complex, distributed systems.
High performance, minimalist Go web framework
Pros of Echo
- Written in Go, offering better performance and easier deployment for Go developers
- More extensive middleware ecosystem and built-in features like JWT authentication
- Simpler API and easier learning curve for beginners
Cons of Echo
- Less mature and battle-tested compared to Cowboy's long history
- Smaller community and potentially fewer resources for troubleshooting
- Limited to Go ecosystem, while Cowboy supports Erlang and Elixir
Code Comparison
Echo (Go):
e := echo.New()
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Logger.Fatal(e.Start(":1323"))
Cowboy (Erlang):
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]}
]),
{ok, _} = cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
).
Both frameworks provide straightforward ways to set up a basic HTTP server, but Echo's syntax is more concise and arguably easier to read for those familiar with Go. Cowboy's approach reflects Erlang's syntax and conventions, which may be less intuitive for developers coming from other languages.
⚡️ Express inspired web framework written in Go
Pros of Fiber
- Written in Go, offering better performance and concurrency handling
- More extensive middleware ecosystem and built-in features
- Simpler API and easier learning curve for developers familiar with Express.js
Cons of Fiber
- Less mature and battle-tested compared to Cowboy
- Smaller community and potentially fewer long-term maintenance resources
- May not be as suitable for highly complex, distributed systems
Code Comparison
Cowboy (Erlang):
Dispatch = cowboy_router:compile([
{'_', [{"/", hello_handler, []}]}
]),
{ok, _} = cowboy:start_clear(my_http_listener,
[{port, 8080}],
#{env => #{dispatch => Dispatch}}
).
Fiber (Go):
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":8080")
Both frameworks provide simple ways to set up a basic HTTP server, but Fiber's syntax is more concise and reminiscent of Express.js, which may be more familiar to many developers. Cowboy's Erlang-based approach offers powerful concurrency features but may have a steeper learning curve for those not familiar with Erlang/OTP.
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
Pros of actix-web
- Higher performance and lower latency due to Rust's efficiency and Actix's actor-based architecture
- More modern and actively maintained, with frequent updates and a growing ecosystem
- Strong type safety and memory safety guarantees inherent to Rust
Cons of actix-web
- Steeper learning curve, especially for developers new to Rust
- Smaller community and fewer resources compared to Cowboy's Erlang ecosystem
- Less mature and battle-tested in production environments
Code Comparison
Cowboy (Erlang):
init(Req0, State) ->
Req = cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain">>
}, <<"Hello World!">>, Req0),
{ok, Req, State}.
actix-web (Rust):
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello World!")
}
Both frameworks offer concise ways to define routes and handle requests, but actix-web leverages Rust's async/await syntax for potentially better performance in high-concurrency scenarios.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
= Cowboy
Cowboy is a small, fast and modern HTTP server for Erlang/OTP.
== Goals
Cowboy aims to provide a complete HTTP stack in a small code base. It is optimized for low latency and low memory usage, in part because it uses binary strings.
Cowboy provides routing capabilities, selectively dispatching requests to handlers written in Erlang.
Because it uses Ranch for managing connections, Cowboy can easily be embedded in any other application.
Cowboy is clean and well tested Erlang code.
== Online documentation
- https://ninenines.eu/docs/en/cowboy/2.12/guide[User guide]
- https://ninenines.eu/docs/en/cowboy/2.12/manual[Function reference]
== Offline documentation
- While still online, run
make docs
- User guide available in
doc/
in PDF and HTML formats - Function reference man pages available in
doc/man3/
anddoc/man7/
- Run
make install-docs
to install man pages on your system - Full documentation in Asciidoc available in
doc/src/
- Examples available in
examples/
== Getting help
Top Related Projects
Peace of mind from prototype to production
Fast, unopinionated, minimalist web framework for node.
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
High performance, minimalist Go web framework
⚡️ Express inspired web framework written in Go
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot