Support for Inter-service WebSocket call
Similar to how gofr support calling http services by adding the service using app.AddHTTPService and then accessing the service from the context.
WebSocket interservice calls should be supported in the similar way. As otherwise user would need to do dependency injection for all their WebSocket services.
Please assign this issue to me
@rigved-telang assigned the issue to you.
Before proceeding with the implementation it will be really helpful if we can discuss on the plan to implement this. What are your thoughts? How are you gonna proceed with it?
Sure. Have been going through the code, needed some time to understand it. Will put my plan of implementation here for review before starting with dev. Will need a few days for this, hope thats okay.
@rigved-telang sure you can take your time. Will look forward to discussing with you about implementation
Hey @Umang01-hash, this is what I have thought of so far
pkg/gofr/container/container.go
// Map to store map of services with their connection strings
type Container struct {
...
WSManager *websocket.Manager
...
}
pkg/gofr/gofr.go
// Function to add WebSocket connection to the websocket Manager
// AddWSService registers WS connection in container.
func (a *App) AddWSService(serviceName, serviceAddress string, options ...service.Options) {
if a.container.WSManager == nil {
a.container.WSManager = *websocket.New()
}
if _, ok := a.container.WSManager[serviceName]; ok {
a.container.Debugf("Service already registered Name: %v", serviceName)
}
a.container.Services[serviceName] = service.NewWSService(...)
}
I am having trouble as to how and where I can call the Upgrade function in WSUpgrader to get the Connection struct as the Upgrade function requires w http.ResponseWriter, r *http.Request, responseHeader http.Header as the arguments, how do I pass these arguments. Let me know if you think the approach is correct or not, also do answer my doubt about the Upgrade function.
Hey @rigved-telang. Your approach to using the Container struct to manage WebSocket services is correct. And regarding the Upgrade function call and passing the required arguments I need to think and figure it out. Please lend me some time will get back to you.
@rigved-telang So having a look at the core implementation of websockets in GoFr you'll find that we have a middleware WSHandlerUpgrade that upgrades the incoming request to websocket connection.
We can't directly call the Upgrade function of WSUpgrader because it will require raw ResponseWriter and Request which are wrapped by our GoFr.
Acc to me the flow of implementation can be after adding the ws service in AddWSService we will just put the entry in the WSService mapping inside container but we will establish the connection when the user calls GetWSService which can then return the established connection.
We need to have methods on our Connection struct to Read and Write messages. The thing to be taken care of here is how you will manage these connections ?
I tried a basic raw (but incomplete) implementation in this branch: https://github.com/gofr-dev/gofr/compare/en/intersvc_websocket
Maybe you can have a look.
Cool, I'll look into this and let you know. Thanks
@rigved-telang , Any updates on this discussion ?
No sorry, I haven't been able to work on it. Have been jam-packed with office work. If anybody else wants to pick this issue, do let them, I don't think I will be able to progress this. Apologies for the inconvenience.