Closed chase-lewis-akasa closed 2 years ago
Hi, what do you mean with "create a client websocket to a specific domain"?
Hey @antoniodipinto
I mean, like in the frontend you can do
ws = new WebSocket("wss://somedomain.com")
I need to create a websocket connection to another server and register it with the framework. I will be doing this when a client connection connects to the backend. So there the messages will go Client -> Proxy -> Backend.
This doesn't compile but hopefully this gives a better idea of what I'm trying to do. I have multiple questions in here. Any help would be greatly appreciated.
package follower
import (
"encoding/json"
"fmt"
"github.com/antoniodipinto/ikisocket"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
"log"
)
func main() {
// The key for the map is message.to
clients := make(map[string]string)
// Start a new Fiber application
app := fiber.New()
// Setup the middleware to retrieve the data sent in first GET request
app.Use(func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
c.Locals("allowed", true)
return c.Next()
}
return fiber.ErrUpgradeRequired
})
// On message event
ikisocket.On(ikisocket.EventMessage, func(ep *ikisocket.EventPayload) {
echoUUID := ep.Kws.GetAttribute("echo-socket").(string)
err := ep.Kws.EmitTo(echoUUID, ep.Data)
if err != nil {
fmt.Println(err)
}
})
// On disconnect event
ikisocket.On(ikisocket.EventClose, func(ep *ikisocket.EventPayload) {
echoUUID := ep.Kws.GetAttribute("echo-socket").(string)
err := ep.Kws.EmitTo(echoUUID,[]byte(ikisocket.CloseMessage)) //This doesn't compile. How can I close 'echo-connection'?
})
// On error event
ikisocket.On(ikisocket.EventError, func(ep *ikisocket.EventPayload) {
fmt.Println(fmt.Sprintf("Error event - User: %s", ep.Kws.GetStringAttribute("user_id")))
})
app.Get("/ws/:id", ikisocket.New(func(kws *ikisocket.Websocket) {
backendSocket := &ikisocket.Websocket{ ws: websocket.Conn{} } //How do I fill this out? This won't compile
backendSocket.SetUUID("random_string") //Helper function within the library to auto do this?
//Register with the ickisocket framework a new socket?
ep.Kws.SetAttribute("echo-socket",backendSocket.GetUUID())
backendSocket.SetAttribute("echo-socket",ep.Kws.GetUUID())
}))
log.Fatal(app.Listen(":3000"))
}
In theory you can do that creating a new WS Client that connect to the second WS server. So you can proxy the payload forward and backward from the last server.
This use case is kind of really vertical and is not something that Ikisocket can provide right now, being a wrapper over Fiber WS.
Probably your idea/request, looks like a feature request
Thank you
Is your feature request related to a problem? Please describe. I'm trying to use this library to simplify setting up a proxy. To do that on 'EventConnect' I need to open a new connection. Then I can just replicate messages from each source back to the other by setting the UUID as a stored value on them.
Describe the solution you'd like Just wondering if there is a way to programmatically create a client websocket to a specific domain.