All Projects â†’ gofiber â†’ Websocket

gofiber / Websocket

Licence: mit
🧬 WebSocket middleware for Fiber

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Websocket

Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (-15.25%)
Mutual labels:  middleware
Django Channels React Multiplayer
turn based strategy game using django channels, redux, and react hooks
Stars: ✭ 52 (-11.86%)
Mutual labels:  middleware
Koa Useragent
Koa user-agent middleware
Stars: ✭ 54 (-8.47%)
Mutual labels:  middleware
Guzzle Stopwatch Middleware
A Guzzle Stopwatch Middleware
Stars: ✭ 50 (-15.25%)
Mutual labels:  middleware
Aspnetcore Request Decompression
HTTP request decompression middleware for ASP.NET Core
Stars: ✭ 51 (-13.56%)
Mutual labels:  middleware
Express Fileupload
Simple express file upload middleware that wraps around busboy
Stars: ✭ 1,069 (+1711.86%)
Mutual labels:  middleware
Graphql Factory
A toolkit for building GraphQL
Stars: ✭ 44 (-25.42%)
Mutual labels:  middleware
Http Proxy Middleware
âš¡ The one-liner node.js http-proxy middleware for connect, express and browser-sync
Stars: ✭ 8,730 (+14696.61%)
Mutual labels:  middleware
Proxykit
A toolkit to create code-first HTTP reverse proxies on ASP.NET Core
Stars: ✭ 1,063 (+1701.69%)
Mutual labels:  middleware
Gin Glog
Gin middleware to use glog
Stars: ✭ 53 (-10.17%)
Mutual labels:  middleware
Guzzle Cache Middleware
A Guzzle Cache middleware
Stars: ✭ 50 (-15.25%)
Mutual labels:  middleware
Redux Query
A library for managing network state in Redux
Stars: ✭ 1,055 (+1688.14%)
Mutual labels:  middleware
Rainbow
An Express router middleware for RESTful API base on file path.
Stars: ✭ 53 (-10.17%)
Mutual labels:  middleware
Authorization
PSR7 Middleware for authorization
Stars: ✭ 50 (-15.25%)
Mutual labels:  middleware
Dragon
âš¡A powerful HTTP router and URL matcher for building Deno web servers.
Stars: ✭ 56 (-5.08%)
Mutual labels:  middleware
Looli
a tiny web framework
Stars: ✭ 45 (-23.73%)
Mutual labels:  middleware
Condor Framework
Framework for building GRPC services in Node JS. Include middleware, and more.
Stars: ✭ 52 (-11.86%)
Mutual labels:  middleware
Helmet
Help secure Express apps with various HTTP headers
Stars: ✭ 8,648 (+14557.63%)
Mutual labels:  middleware
Momi
Monadic middleware
Stars: ✭ 57 (-3.39%)
Mutual labels:  middleware
Redux Electron Ipc
Redux Electron IPC Middleware
Stars: ✭ 54 (-8.47%)
Mutual labels:  middleware

WebSocket

Release Discord Test Security Linter

Based on Fasthttp WebSocket for Fiber with available *fiber.Ctx methods like Locals, Params, Query and Cookies.

Install

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/websocket/v2

Example

package main

import (
	"log"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/websocket/v2"
)

func main() {
	app := fiber.New()

	app.Use("/ws", func(c *fiber.Ctx) error {
		// IsWebSocketUpgrade returns true if the client
		// requested upgrade to the WebSocket protocol.
		if websocket.IsWebSocketUpgrade(c) {
			c.Locals("allowed", true)
			return c.Next()
		}
		return fiber.ErrUpgradeRequired
	})

	app.Get("/ws/:id", websocket.New(func(c *websocket.Conn) {
		// c.Locals is added to the *websocket.Conn
		log.Println(c.Locals("allowed"))  // true
		log.Println(c.Params("id"))       // 123
		log.Println(c.Query("v"))         // 1.0
		log.Println(c.Cookies("session")) // ""

		// websocket.Conn bindings https://pkg.go.dev/github.com/fasthttp/websocket?tab=doc#pkg-index
		var (
			mt  int
			msg []byte
			err error
		)
		for {
			if mt, msg, err = c.ReadMessage(); err != nil {
				log.Println("read:", err)
				break
			}
			log.Printf("recv: %s", msg)

			if err = c.WriteMessage(mt, msg); err != nil {
				log.Println("write:", err)
				break
			}
		}

	}))

	log.Fatal(app.Listen(":3000"))
	// Access the websocket server: ws://localhost:3000/ws/123?v=1.0
	// https://www.websocket.org/echo.html
}

Note with cache middleware

If you get the error websocket: bad handshake when using the cache middleware, please use config.Next to skip websocket path.

app := fiber.New()
app.Use(cache.New(cache.Config{
		Next: func(c *fiber.Ctx) bool {
			return strings.Contains(c.Route().Path, "/ws")
		},
}))

app.Get("/ws/:id", websocket.New(func(c *websocket.Conn) {}))
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].