photonMelodyAdapter

package module
v0.0.0-...-54a4523 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 7 Imported by: 0

README

photon-melody-adapter

Photon Melody Adapter is a plug-and-play WebSocket adapter for the Photon framework, based on the excellent Melody WebSocket library.

This adapter allows you to integrate real-time WebSocket support into your Photon applications using Melody’s event-driven API.


✨ Features

  • 🎧 Built on top of Melody — lightweight, elegant WebSocket handling
  • 🔌 Seamlessly integrates with Photon’s adapter system
  • 🔁 Simple event-based interface (On, Emit, etc.)
  • 🧹 Fully decoupled and modular

📦 Installation

go get github.com/smtdfc/photon-melody-adapter

🚀 Usage Example

1. Add to your Photon app
package app

import (
  "github.com/smtdfc/photon"
  "github.com/smtdfc/photon-melody-adapter"
)

func Init() *photon.App {
  socketAdapter,_ := photonMelodyAdapter.Init(),
  
  app := photon.NewApp()
  app.Adapter.UseSocketAdapter(socketAdapter)
  
  // Init modules
  InitModule(app)
  
  return app
}

Inside internal/your-module/routes.go:


func (m *HelloModule) InitRoute(){
  
  socketController := photon.InitSocketController(
    m.App,
    m.Module,
  )
  
  socketController.On("hello",func(client *photon.SocketSession, msg *photon.SocketEventMessage){
    fmt.Println("Client send")
    fmt.Println(msg)
  })
  
  httpController := photon.InitHttpController(
    m.App,
    m.Module,
  )
  
  httpController.RouteSocket("/socket") // Register route for socket use 
}

2. Connect via browser
const socket = new WebSocket("ws://localhost:3000/ws");
socket.onmessage = (event) => console.log("Message from server:", event.data);
socket.send(JSON.stringify({
      event: "hello", // event mame
      data: { }
}));

🤝 Contributing

Pull requests, suggestions, and issues are welcome! Feel free to open a discussion or PR anytime.


📜 License

This adapter is licensed under the MIT License. It integrates the Melody WebSocket library, which is distributed under the BSD-2-Clause license.

Please refer to THIRD_PARTY_LICENSES/melody-LICENSE for full license text.


🙏 Acknowledgments

  • Melody — Minimalist WebSocket library for Go
  • Photon — Modular backend framework for Go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MelodyAdapter

type MelodyAdapter struct {
	Instance *melody.Melody

	Clients map[string]*photon.SocketSession
	Rooms   map[string]*photon.SocketRoom
	// contains filtered or unexported fields
}

func Init

func Init() (*MelodyAdapter, *melody.Melody)

func (*MelodyAdapter) CreateRoom

func (m *MelodyAdapter) CreateRoom(id string) (*photon.SocketRoom, error)

func (*MelodyAdapter) Emit

func (*MelodyAdapter) GetName

func (m *MelodyAdapter) GetName() string

func (*MelodyAdapter) GetRoom

func (m *MelodyAdapter) GetRoom(id string) *photon.SocketRoom

func (*MelodyAdapter) HTTPHandler

func (m *MelodyAdapter) HTTPHandler() func(http.ResponseWriter, *http.Request)

func (*MelodyAdapter) Init

func (m *MelodyAdapter) Init() error

func (*MelodyAdapter) JoinRoom

func (m *MelodyAdapter) JoinRoom(id string, client *photon.SocketSession) error

func (*MelodyAdapter) Listen

func (m *MelodyAdapter) Listen(port string) error

func (*MelodyAdapter) On

func (m *MelodyAdapter) On(event string, handler photon.SocketEventHandler)

func (*MelodyAdapter) Start

func (m *MelodyAdapter) Start() error

func (*MelodyAdapter) Stop

func (m *MelodyAdapter) Stop() error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL