Comments (12)
Thanks for opening your first issue here! π Be sure to follow the issue template!
from recipes.
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!")
})
from recipes.
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?
https://github.com/maverickVision/testws
from recipes.
@maverickVision I am not free at the moment, maybe you could take a look at this repo that address the same issue.
Checkout on problem
branch.
from recipes.
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?
from recipes.
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))
}
from recipes.
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?
@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.
from recipes.
@maverickVision I am not free at the moment, maybe you could take a look at this repo that address the same issue.
Checkout on
problem
branch.
@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.
from recipes.
@maverickVision You don't need to access to responseWriter. You could use https://github.com/gofiber/websocket.
from recipes.
@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
app.Get("/ws", websocket.New(func(c *websocket.Conn) {
ws.ServeWs(hub, c, nc, subject)
}))
ws.go
func ServeWs(hub *Hub, conn *websocket.Conn, nc *nats.Conn, subject string) {
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
// subscribe nats
sub, err := nc.Subscribe(subject, func(m *nats.Msg) {
log.Println(string(m.Data))
client.hub.broadcast <- m.Data
})
if err != nil {
log.Fatal(err)
}
nc.Flush()
if err := nc.LastError(); err != nil {
log.Fatal(err)
}
client.natsSub = sub
client.hub.register <- client
// Allow Connection of memory referenced by the calloer by doing all work in new goroutines
go client.writePump()
go client.readPump()
}
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 make start-api
console log:
2020/10/12 13:53:44 ws.go:64: Current connected ws client: 1
2020/10/12 13:53:44 ws.go:71: Unsubscribe
2020/10/12 13:53:44 ws.go:74: Current connected ws client: 0
panic: runtime error: invalid memory address or nil pointer dereference
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x149e03e]
goroutine 8 [running]:
github.com/fasthttp/websocket.(*Conn).Close(...)
/Users/marcelo/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:344
github.com/maverickvision/testws/internal/ws.(*Client).writePump.func1(0xc00007e320, 0xc00028a0a0)
/Users/marcelo/playground/golang/testws/internal/ws/ws.go:160 +0x3e
panic(0x150bc00, 0x1a06520)
/usr/local/go/src/runtime/panic.go:969 +0x166
github.com/fasthttp/websocket.(*Conn).SetWriteDeadline(...)
/Users/marcelo/go/pkg/mod/github.com/fasthttp/[email protected]/conn.go:781
github.com/maverickvision/testws/internal/ws.(*Client).writePump(0xc00028a0a0)
/Users/marcelo/playground/golang/testws/internal/ws/ws.go:166 +0x311
created by github.com/maverickvision/testws/internal/ws.ServeWs
/Users/marcelo/playground/golang/testws/internal/ws/ws.go:221 +0x240
I printed the conn
and client
variables on ws.go, and they are not null, what could it be?
I've updated my repo with the latest code, if you can help.
from recipes.
@maverickVision I created a simplified example,using websocket in Fiber. You may take a look at this repo
from recipes.
Closing due to inactivity, feel free to re-open.
from recipes.
Related Issues (20)
- fiber seems not support cancelContext like net/http π€ HOT 2
- How to modify response body through fiber.Static.ModifyResponse? π€ HOT 2
- π Bug on the auth-jwt recipe, log in with email doesn't work HOT 2
- π SvelteKit Recipe needs an update HOT 8
- π€SSE example: how can I add an endpoint to fire en event HOT 2
- π€ websocket-chat: send websocket messages in parallel HOT 6
- π€ Close Database connection HOT 1
- I want the JWT data in my route, how to Get that? HOT 2
- π₯ Any fiber Client recipes?? HOT 8
- π auth-jwt project panics in UpdateUser handler HOT 4
- Dependency Dashboard HOT 1
- π BodyParser always returning nil HOT 3
- Bug in Login flow of auth-jwt section of the gofiber/recipesπ HOT 9
- geoip example ,the route in the page request does not match the route in the codeπ HOT 5
- π₯ front-end to drive auth-docker-postgres-jwt template HOT 1
- π₯ I have a nice feature for gorm-mysql listed below: HOT 1
- π€ Issue with interface assignment in recipe "auth-jwt"? panic: interface conversion: interface {} is *jwt.Token, not *jwt.Token (types from different packages) HOT 3
- Is there any advantaje using ctx.UserContext() and pass it into GORM dbInstance.withContext() method? π€ HOT 3
- π₯ fiber-bootstrap should have clear setup instructions HOT 2
- Deprecated Recipe from gRPC Side HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from recipes.