Convert Figma logo to code with AI

ninenines logocowboy

Small, fast, modern HTTP server for Erlang/OTP.

7,256
1,164
7,256
71

Top Related Projects

21,321

Peace of mind from prototype to production

64,773

Fast, unopinionated, minimalist web framework for node.

77,851

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.

29,410

High performance, minimalist Go web framework

33,019

⚡️ Express inspired web framework written in Go

21,546

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

  1. 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!".

  1. 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: ".

  1. 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:

  1. Add Cowboy as a dependency in your rebar.config:
{deps, [
    {cowboy, "2.9.0"}
]}.
  1. Start the Cowboy application in your app.src file:
{applications, [
    kernel,
    stdlib,
    cowboy
]}.
  1. Implement your handlers and set up routing as shown in the code examples above.

  2. Compile and run your application:

$ rebar3 compile
$ rebar3 shell

Your Cowboy server should now be running and accessible at http://localhost:8080.

Competitor Comparisons

21,321

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.

64,773

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.

77,851

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.

29,410

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.

33,019

⚡️ 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.

21,546

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

= 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

== 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/ and doc/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