-
-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to broadcast a WS response from a HTTPS endpoint?🤔 #57
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! |
Hi @maverickVision, I think you need separate websocket for that. And your API server connects to your websocket app.Get("/operation", func (c *fiber.Ctx) error {
ws.send('message')
return c.SendString("done!")
}) |
Hi @WLun001, I've created this simple repo with the minimal working structure using fiber and it's websockets plugin, could you help me modify it to get it working? |
@maverickVision I am not free at the moment, maybe you could take a look at this repo that address the same issue. Checkout on |
Thanks @WLun001, I'll take a look. But I already saw that you are using the gin framework. Since I'm still beggining with Go, do you think it's better to learn gin instead of fiber? |
I'm not sure if I fully understand the question, but are you trying to run a websocket server with TLS? package main
import (
"crypto/tls"
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/websocket/v2"
)
func main() {
// Fiber instance
app := fiber.New()
// Websocket upgrade
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
var (
mt int
msg []byte
err error
)
for {
if mt, msg, err = c.ReadMessage(); err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", msg)
if err = c.WriteMessage(mt, msg); err != nil {
log.Println("write:", err)
break
}
}
}))
// Load tls certificate
cer, err := tls.LoadX509KeyPair("./certs/ssl.cert", "./certs/ssl.key")
if err != nil {
log.Fatal(err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
// Create custom listener
ln, err := tls.Listen("tcp", ":443", config)
if err != nil {
panic(err)
}
// Access the websocket server: wss://localhost:443/ws
// https://www.websocket.org/echo.html
log.Fatal(app.Listener(ln))
} |
@maverickVision If you coming from Express, Fiber would be good choice, it has familiar APIs. Any framework will do, or without framework. That's my personal opinion. |
@WLun001 I'm following your code from this repo, on the solution brach. The problem with fiber is that I can't access the responseWriter from fiber Context, in order to upgrade the connection inside the ServeWs method... and according to this issue #721, it is really not possible. I guess I will need to rewrite my project with Gin just because of this issue. |
@maverickVision You don't need to access to responseWriter. You could use https://github.com/gofiber/websocket. |
I've made the following changes: main.go
ws.go
I've switched the conn variable from the upgraded gin context to fiber websocket conn. All services gone up without issues (using the same commands you use in your code), but when I open a browser tab on localhost:8080, I get the following error on the
I printed the I've updated my repo with the latest code, if you can help. |
@maverickVision I created a simplified example,using websocket in Fiber. You may take a look at this repo |
Closing due to inactivity, feel free to re-open. |
Hello everyone, I'm starting with websockets and I would like to know if it is possible to send a WS notification after some action is done inside a HTTPS endpoint. For example, I want to notify some users (I still need to figure out how to broadcast just to some users) when a new User is created on the admin panel. A POST request is made to the endpoint /agent and I would like to use websockets to send this new data and populate a data grid when a certain user is on the Agents creation screen.
I don't know if its clear, but is it possible to be made?
Thanks in advance.
The text was updated successfully, but these errors were encountered: