Coral

package module
v0.0.0-...-9c2a099 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2021 License: MIT Imports: 5 Imported by: 0

README

Coral

Coral is a small library which aims to have the same workflow as SocketIO.

Requirements

  • golang 1.16+

Getting Started

Install the library in your go project :

go get -u github.com/go-gems/coral

Then in your projects you have to implement a web server and on a route call the Coral.handle method. For example on a vanilla http server :

package main

import (
	"github.com/go-gems/coral"
	"net/http"
)

func main() {

	// handle coral requests on /ws.
	http.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {
		if err := Coral.Handle(writer, request); err != nil {
			panic(err)
		}
	})
	
	// serve a public directory on /
	http.Handle("/", http.FileServer(http.Dir("./public")))

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

the example folder contains the implement with gin.

You can now listen on every websocket connection:

//event to do when user joins the server.
Coral.OnConnection(func(conn *Coral.Conn) error {
    log.Println("connection established")
    
    //Broadcast to every user.
    Coral.Broadcast("user:joined","Somebody joined!")
    
    //conn Object represents a single user connected.
    //When conn sends a "says" type (or anything you want type)
    conn.On("says", func(content interface{}) error {
        //broadcast to all except sender
         conn.Broadcast("listen", content)
         return nil
    })
    // When conn sends a "ping" message.
    conn.On("ping", func(content interface{}) error {
        //Forward to all except conn.
        return conn.Emit("pong", content)
    })
    return nil
})

Javascript side, you can use the Coral.js which is the beginning of a library - and thus will be published as a npm package. Example usage :

<!doctype html>
<html>
    <body>
        <h1>Coral Demo.</h1>
        <script type="module" src="script.js"></script>
    </body>
</html>

and in script.js.

import Coral from "./Coral.js";

//coral communication instance
//default connection to same host/ws
const coral = new Coral()
// if needed
// const coral = new Coral({url: "ws://test.com/something"})

//on connection open
coral.onOpen = function () {
    console.log("connection established");
    
    // on message recieved on listen
    coral.on("listen", (msg) => {
        console.log("received message ", msg)
    })
    
    coral.on("user:joined", (msg)=>{
        console.log(msg);
        coral.emit("says","Hello new User.");

    })
    coral.on("pong", (msg) => {
        console.log("PONG", msg)
    })

    coral.emit("ping","Hello");
}

And that's all for now!

Contribution

If you like the project, feel free to contribute, there are plenty of things to do :

  • Transform coral.js as a npm library
  • Add Channels management (WIP)

So every PR is welcome.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Broadcast

func Broadcast(t string, content interface{})

func Handle

func Handle(w http.ResponseWriter, r *http.Request) error

func OnClose

func OnClose(callback func(conn *Conn))

func OnConnection

func OnConnection(callback func(conn *Conn) error)

Types

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

func (*Channel) AddMember

func (c *Channel) AddMember(id string, conn *Conn)

func (*Channel) Broadcast

func (c *Channel) Broadcast(t string, content interface{})

func (*Channel) GetMembers

func (c *Channel) GetMembers() map[string]*Conn

func (*Channel) RemoveMember

func (c *Channel) RemoveMember(id string)

type Conn

type Conn struct {
	*websocket.Conn
	ID uuid.UUID
	// contains filtered or unexported fields
}

func (*Conn) Broadcast

func (c *Conn) Broadcast(t string, content interface{})

func (*Conn) BroadcastToChannel

func (c *Conn) BroadcastToChannel(name string, t string, content interface{})

func (*Conn) Close

func (c *Conn) Close()

func (*Conn) Emit

func (c *Conn) Emit(t string, content interface{}) error

func (*Conn) GetChannels

func (c *Conn) GetChannels()

func (*Conn) On

func (c *Conn) On(t string, callback func(content interface{}) error)

Jump to

Keyboard shortcuts

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