Zurück zum Ranking

olahol/melody

Go

:notes: Minimalist websocket framework for Go

websocketframeworkwebsocket-frameworkminimalistexamplegogolang
Sterne-Wachstum
Sterne
4.1k
Forks
372
Wochenwachstum
Issues
13
1k2k3k4k
Mai 2015Jan. 2019Okt. 2022Juli 2026
ArtefakteGo Modulesgo get github.com/olahol/melody
README

melody

Build Status Codecov Go Report Card GoDoc

:notes: Minimalist websocket framework for Go.

Melody is websocket framework based on github.com/gorilla/websocket that abstracts away the tedious parts of handling websockets. It gets out of your way so you can write real-time apps. Features include:

  • Clear and easy interface similar to net/http or Gin.
  • A simple way to broadcast to all or selected connected sessions.
  • Message buffers making concurrent writing safe.
  • Automatic handling of sending ping/pong heartbeats that timeout broken sessions.
  • Store data on sessions.

Install

go get github.com/olahol/melody

Example: chat

Chat

package main

import (
	"net/http"

	"github.com/olahol/melody"
)

func main() {
	m := melody.New()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "index.html")
	})

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		m.HandleRequest(w, r)
	})

	m.HandleMessage(func(s *melody.Session, msg []byte) {
		m.Broadcast(msg)
	})

	http.ListenAndServe(":5000", nil)
}

Example: gophers

Gophers

package main

import (
	"fmt"
	"net/http"
	"sync/atomic"

	"github.com/olahol/melody"
)

var idCounter atomic.Int64

func main() {
	m := melody.New()

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		http.ServeFile(w, r, "index.html")
	})

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		m.HandleRequest(w, r)
	})

	m.HandleConnect(func(s *melody.Session) {
		id := idCounter.Add(1)

		s.Set("id", id)

		s.Write([]byte(fmt.Sprintf("iam %d", id)))
	})

	m.HandleDisconnect(func(s *melody.Session) {
		if id, ok := s.Get("id"); ok {
			m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s)
		}
	})

	m.HandleMessage(func(s *melody.Session, msg []byte) {
		if id, ok := s.Get("id"); ok {
			m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s)
		}
	})

	http.ListenAndServe(":5000", nil)
}

More examples

Documentation

Contributors

FAQ

If you are getting a 403 when trying to connect to your websocket you can change allow all origin hosts:

m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
Ähnliche Repositories
louislam/uptime-kuma

A fancy self-hosted monitoring tool

JavaScriptnpmMIT Licenseuptimemonitoring
uptime.kuma.pet
89.4k8.1k
hoppscotch/hoppscotch

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

TypeScriptnpmMIT Licenseapiapi-client
hoppscotch.io
79.8k6k
socketio/socket.io

Bidirectional and low-latency communication for every platform

TypeScriptnpmMIT Licensejavascriptnodejs
socket.io
63.2k10.2k
mitmproxy/mitmproxy

An interactive TLS-capable intercepting HTTP proxy for penetration testers and software developers.

PythonPyPIMIT Licensepythonsecurity
mitmproxy.org
44.4k4.6k
curl/curl

A command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, MQTTS, POP3, POP3S, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP, WS and WSS. libcurl offers a myriad of powerful features

COtherhttphttps
curl.se
42.4k7.3k
labstack/echo

High performance, minimalist Go web framework

GoGo ModulesMIT Licensegoecho
echo.labstack.com
32.5k2.3k
kataras/iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:

GoGo ModulesBSD 3-Clause "New" or "Revised" Licensegoiris
iris-go.com
25.6k2.4k
gorilla/websocket

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.

GoGo ModulesBSD 2-Clause "Simplified" Licensegowebsocket
gorilla.github.io
24.8k3.6k
louislam/dockge

A fancy, easy-to-use and reactive self-hosted docker compose.yaml stack-oriented manager

TypeScriptnpmMIT Licensedockerdocker-compose
dockge.kuma.pet
23.9k789
websockets/ws

Simple to use, blazing fast and thoroughly tested WebSocket client and server for Node.js

JavaScriptnpmMIT Licensewebsocketjavascript
22.8k2.6k
yudai/gotty

Share your terminal as a web application

GoGo ModulesMIT Licensettyterminal
19.5k1.4k
swoole/swoole-src

🚀 Coroutine-based concurrency library for PHP

C++Apache License 2.0swoolecoroutines
swoole.com
18.9k3.1k