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

EmitTo not working? #24

Closed omdxp closed 2 years ago

omdxp commented 2 years ago

Describe the bug It seems only Broadcast() and Emit() are working but only for ikisocket.EventMessage. But when using EmitTo() there is nothing.

To Reproduce Steps to reproduce the behavior:

  1. Server code:
    
    package main

import ( "fmt"

"github.com/antoniodipinto/ikisocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"

)

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

app.Use("/ws", func(c *fiber.Ctx) error {
    if websocket.IsWebSocketUpgrade(c) {
        c.Locals("allowed", true)
        return c.Next()
    }
    return fiber.ErrUpgradeRequired
})

ikisocket.On(ikisocket.EventConnect, func(ep *ikisocket.EventPayload) {
    fmt.Printf("Client %s connected\n", ep.Kws.GetUUID())
    ep.Kws.Emit([]byte("Hello from server"))
})

ikisocket.On(ikisocket.EventDisconnect, func(ep *ikisocket.EventPayload) {
    fmt.Printf("Client %s disconnected\n", ep.Kws.GetUUID())
    ikisocket.Broadcast([]byte("Client disconnected"))
})

ikisocket.On("orders", func(ep *ikisocket.EventPayload) {
    fmt.Printf("Client %s sent orders\n", ep.Kws.GetUUID())
    ikisocket.EmitTo(ikisocket.EventMessage, []byte("hey there"))
})

app.Post("/orders", func(c *fiber.Ctx) error {
    ikisocket.EmitTo("orders", c.Body(), ikisocket.TextMessage)
    ikisocket.Fire("orders", []byte("order added"))
    return c.SendString("orders")
})

app.Get("/ws", ikisocket.New(func(kws *ikisocket.Websocket) {
    kws.Broadcast([]byte("Client connected"), true, ikisocket.TextMessage)
    kws.Emit([]byte("Welcome to the websocket"))
    kws.Emit([]byte("You are the best"))
    kws.EmitTo("orders", []byte("hey there"))
    kws.EmitTo(ikisocket.EventMessage, []byte("hey there"))
}))

app.Listen(":8080")

}

client code:
```js
import WebSocket from "ws";

let ws = new WebSocket("ws://localhost:8080/ws");

ws.on("message", (data) => {
  console.log("message");
  data = Buffer.from(data).toString();
  console.log(data);
});

ws.on("error", (error) => {
  console.log(error);
});

ws.on("close", (code, reason) => {
  console.log("close");
  console.log(code, Buffer.from(reason).toString());
});

ws.on("orders", (data) => {
  console.log("orders");
  data = Buffer.from(data).toString();
  console.log(data);
});
  1. From the client I get only the data from predefined event that are fired from the server by Emit() or Broadcast(). Not getting anything from EmitTo()

Expected behavior EmitTo() should work for predefined events as well as custom events.

antoniodipinto commented 2 years ago

Hi Omar,

The EmitTo method requires the UUID string of a specific client as a first parameter. From what I can see there you are sending the message to "orders" that is not a UUID

Store the UUID of the specific client in a list, and then use EmitTo with that UUID

I see also that you're listening for the "orders" event in the client, and this will not happen. Ikisocket allow you to handle your events at server level, not on the client level. This means that your

ws.on("orders") will not work, because orders is not a client event

Hope this will help

omdxp commented 2 years ago

Hey @antoniodipinto thanks for the clarification, I thought that EmitTo is meant to send to data to custom event.

In that case is there any way to send data on custom event?

antoniodipinto commented 2 years ago

One idea that comes to my mind is to do it here:

ws.on("message", (data) => {
  console.log("message received". data);
  data = Buffer.from(data).toString();
  console.log(data);
});
app.Post("/orders", func(c *fiber.Ctx) error {
    ikisocket.Emit(c.Body(), ikisocket.TextMessage)
    return c.SendString("orders")
})

If you want to add more possibilities, you can send back to the client an JSON object like this:

{
    "event":"<event_name_here>",
    "data":"<your_data_here>"
}

So then you can check which kind of event you have

omdxp commented 2 years ago

Yeah seems doable, I hope you can add this functionality to emit to specific events. Kinda like socketio

antoniodipinto commented 2 years ago

@Omar-Belghaouti from what I know you can do that in Socket.io only if you use the socket.io client https://github.com/socketio/socket.io-client not the standard WS class in JS. Right not I'm not thinking of developing also a client version for ikisocket, but maybe in the future, who knows :)

Thank you for your feedback! I'll close this issue