antoniodipinto / ikisocket

🧬 WebSocket wrapper with event management for Fiber https://github.com/gofiber/fiber. Based on Fiber WebSocket and inspired by Socket.io
MIT License
123 stars 21 forks source link

Fire event outside a handler #11

Closed dmorawetz closed 3 years ago

dmorawetz commented 3 years ago

Is your feature request related to a problem? Please describe. I would like to fire a custom event, when I get a HTTP request.

Describe the solution you'd like I'd like to do something like ikisocket.Fire("customEvent", "payload") and ikisocket.Broadcast("message")

Describe alternatives you've considered Alternatively I would have to keep my own map / slice of connected websockets and reimplement your Fire and Broadcast methods.

antoniodipinto commented 3 years ago

Alternatively I would have to keep my own map / slice of connected websockets and reimplement your Fire and Broadcast methods.

You can already do it, if you check the chat example you can see there the connected clients map

For the feature request I will take a look over it

dmorawetz commented 3 years ago

In the chat example the message arrives via websocket, so the EmitTo happens in an ikisocket.On handler.

My scenario is I have an endpoint /events where I want to handle POST requests. When a new event is created I would like to notify all WS clients about the new event.

To elaborate on my example use

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

   // setup ikisocket

    app.Post("/events", func (c *fiber.Ctx) error {
        // store new event in DB

       // notify all connected clients
       ikisocket.Broadcast("new event as json")

        return nil
    })

    log.Fatal(app.Listen(":3000"))
}

I could provide a pull request, if you approve this idea.