Giter VIP home page Giter VIP logo

websocket-mux's Introduction

Muxr: Efficient WebSocket Multiplexing for Go

Muxr is a Go package designed to simplify WebSocket communication by offering efficient multiplexing capabilities. It enables you to manage multiple streams over a single WebSocket connection, where each stream can handle multiple requests and responses, effectively streamlining communication between clients and servers.

Note: If user connect to a muxr server with a regular WebSocket client, the muxr server handles it in single stream mode.

Features

  • Stream Multiplexing: Handle multiple streams concurrently over a single WebSocket connection.
  • Flexible Communication: Each stream can manage multiple requests and responses independently.
  • Simplified Integration: Seamlessly integrate into existing Go applications for WebSocket communication.

Installation

Install Muxr using go get:

go get github.com/onionj/websocket-mux/muxr

Usage

Muxr Server

package main

import (
    "fmt"
    "github.com/onionj/websocket-mux/muxr"
)

func main() {    
    // Create a new Muxr server listening on port 8080.
    server := muxr.NewWsServer(":8080")

    // Handle WebSocket connections on the "/api" endpoint.
    server.Handle("/api", func(stream *muxr.Stream) {
        for {
            // Read data from the client.
            data, err := stream.Read()
            if err != nil {
                fmt.Println("Error reading from client:", err)
                return
            }
            fmt.Println("Server received:", string(data))

            // Send a response back to the client.
            msg := []byte("Pong")
            err = stream.Write(msg)
            if err != nil {
                fmt.Println("Error writing to client:", err)
                return
            }
            fmt.Println("Server sent:", string(msg))
        }
    })

    // Start the Muxr server.
    err := server.ListenAndServe()
    if err != nil {
        fmt.Println("Error starting server:", err)
        return
    }
}

Note: For handling TLS, use: server.ListenAndServeTLS(certFile, keyFile)

Muxr Client

package main

import (
    "fmt"
    "github.com/onionj/websocket-mux/muxr"
)

func main() {
    // Create a new Muxr client connecting to the server.
    client := muxr.NewClient("ws://127.0.0.1:8080/api")
    closerFunc, err := client.StartForever()
    if err != nil {
        panic(err)
    }
    defer closerFunc()

    // Establish a stream for communication with the server.
    stream, err := client.Dial()
    if err != nil {
        fmt.Println("Error establishing stream:", err)
        return
    }
    defer stream.Close()

    // Send a message to the server.
    msg := []byte("Ping")
    if err = stream.Write(msg); err != nil {
        fmt.Println("Error sending message:", err)
        return
    }
    fmt.Println("Client sent:", string(msg))

    // Read the response from the server.
    data, err := stream.Read()
    if err != nil {
        fmt.Println("Error reading response:", err)
        return
    }
    fmt.Println("Client received:", string(data))
}

Examples

Explore additional examples provided in the example directory

License

This project is licensed under the MIT License - see the LICENSE file for details.

websocket-mux's People

Contributors

onionj avatar dependabot[bot] avatar

Stargazers

mehran armiyon avatar Mohammad Mahdi "Mamad" Afshar avatar Masoud avatar  avatar Mark Pashmfouroush avatar kamran avatar Mohsen Barzegar avatar Seyed Arshia Ghaffarian  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.