All Projects â†’ olahol â†’ Melody

olahol / Melody

Licence: bsd-2-clause
🎶 Minimalist websocket framework for Go

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Melody

Hyperf
🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
Stars: ✭ 4,206 (+82.63%)
Mutual labels:  framework, websocket
Concise.css
A CSS framework that's lightweight and easy-to-use. Give up the bloat. Stop tripping over your classes. Be Concise.
Stars: ✭ 941 (-59.14%)
Mutual labels:  framework, minimalist
Framework
Node.js framework
Stars: ✭ 4,139 (+79.72%)
Mutual labels:  framework, websocket
Tonic
A Low Profile Component Framework. Stable, Minimal, Auditable, and Build-Tool-Free.
Stars: ✭ 265 (-88.49%)
Mutual labels:  framework, minimalist
Twig
Twig - less is more's web server for golang
Stars: ✭ 98 (-95.74%)
Mutual labels:  framework, websocket
Cleancppproject
Clean C++ project for you to use. Features: Modern CMake, CPack, Doxygen, PlantUML, Catch Unit testing, static analysis
Stars: ✭ 276 (-88.02%)
Mutual labels:  framework, example
Iogrid
Multiplayer game engine/framework built using SocketCluster and Phaser
Stars: ✭ 455 (-80.24%)
Mutual labels:  framework, websocket
Primus
âš¡ Primus, the creator god of the transformers & an abstraction layer for real-time to prevent module lock-in.
Stars: ✭ 4,302 (+86.8%)
Mutual labels:  framework, websocket
Nodefony Starter
Nodefony Starter Node.js Framework
Stars: ✭ 95 (-95.87%)
Mutual labels:  framework, websocket
Seed Rs Realworld
Exemplary real world application built with Seed
Stars: ✭ 77 (-96.66%)
Mutual labels:  framework, example
Actionhero
Actionhero is a realtime multi-transport nodejs API Server with integrated cluster capabilities and delayed tasks
Stars: ✭ 2,280 (-1%)
Mutual labels:  framework, websocket
Harmonic
A high performance and scalable RTMP live streaming application framework
Stars: ✭ 121 (-94.75%)
Mutual labels:  framework, websocket
Youi
Next generation user interface and application development in Scala and Scala.js for web, mobile, and desktop.
Stars: ✭ 186 (-91.92%)
Mutual labels:  framework, websocket
Horse
Fast, opinionated, minimalist web framework for Delphi
Stars: ✭ 295 (-87.19%)
Mutual labels:  framework, minimalist
Zeva
a modern and minimalist ui framework for building responsive and modern frontends
Stars: ✭ 57 (-97.52%)
Mutual labels:  framework, minimalist
Milligram
A minimalist CSS framework.
Stars: ✭ 9,568 (+315.46%)
Mutual labels:  framework, minimalist
Bolt Js
A framework to build Slack apps using JavaScript
Stars: ✭ 1,971 (-14.42%)
Mutual labels:  framework, websocket
React Redux Typescript Realworld App
RealWorld App implementation based on "react-redux-typescript-guide"
Stars: ✭ 178 (-92.27%)
Mutual labels:  example
Robot
Native cross-platform system automation
Stars: ✭ 178 (-92.27%)
Mutual labels:  framework
Bee
A framework for IOTA nodes, clients and applications in Rust
Stars: ✭ 176 (-92.36%)
Mutual labels:  framework

melody

Build Status Coverage Status GoDoc

🎶 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 ping/pong and session timeouts.
  • Store data on sessions.

Install

go get gopkg.in/olahol/melody.v1

Example: chat

Chat

Using Gin:

package main

import (
	"github.com/gin-gonic/gin"
	"gopkg.in/olahol/melody.v1"
	"net/http"
)

func main() {
	r := gin.Default()
	m := melody.New()

	r.GET("/", func(c *gin.Context) {
		http.ServeFile(c.Writer, c.Request, "index.html")
	})

	r.GET("/ws", func(c *gin.Context) {
		m.HandleRequest(c.Writer, c.Request)
	})

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

	r.Run(":5000")
}

Using Echo:

package main

import (
	"github.com/labstack/echo"
	"github.com/labstack/echo/engine/standard"
	"github.com/labstack/echo/middleware"
	"gopkg.in/olahol/melody.v1"
	"net/http"
)

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

	e.Use(middleware.Logger())
	e.Use(middleware.Recover())

	e.GET("/", func(c echo.Context) error {
		http.ServeFile(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request, "index.html")
		return nil
	})

	e.GET("/ws", func(c echo.Context) error {
		m.HandleRequest(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request)
		return nil
	})

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

	e.Run(standard.New(":5000"))
}

Example: gophers

Gophers

package main

import (
	"github.com/gin-gonic/gin"
	"gopkg.in/olahol/melody.v1"
	"net/http"
	"strconv"
	"strings"
	"sync"
)

type GopherInfo struct {
	ID, X, Y string
}

func main() {
	router := gin.Default()
	mrouter := melody.New()
	gophers := make(map[*melody.Session]*GopherInfo)
	lock := new(sync.Mutex)
	counter := 0

	router.GET("/", func(c *gin.Context) {
		http.ServeFile(c.Writer, c.Request, "index.html")
	})

	router.GET("/ws", func(c *gin.Context) {
		mrouter.HandleRequest(c.Writer, c.Request)
	})

	mrouter.HandleConnect(func(s *melody.Session) {
		lock.Lock()
		for _, info := range gophers {
			s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
		}
		gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
		s.Write([]byte("iam " + gophers[s].ID))
		counter += 1
		lock.Unlock()
	})

	mrouter.HandleDisconnect(func(s *melody.Session) {
		lock.Lock()
		mrouter.BroadcastOthers([]byte("dis "+gophers[s].ID), s)
		delete(gophers, s)
		lock.Unlock()
	})

	mrouter.HandleMessage(func(s *melody.Session, msg []byte) {
		p := strings.Split(string(msg), " ")
		lock.Lock()
		info := gophers[s]
		if len(p) == 2 {
			info.X = p[0]
			info.Y = p[1]
			mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
		}
		lock.Unlock()
	})

	router.Run(":5000")
}

More examples

Documentation

Contributors

  • Ola Holmström (@olahol)
  • Shogo Iwano (@shiwano)
  • Matt Caldwell (@mattcaldwell)
  • Heikki Uljas (@huljas)
  • Robbie Trencheny (@robbiet480)
  • yangjinecho (@yangjinecho)

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 }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].