Skip to content
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

Support for Inter-service WebSocket call #1154

Open
shashank-zopsmart opened this issue Oct 28, 2024 · 10 comments
Open

Support for Inter-service WebSocket call #1154

shashank-zopsmart opened this issue Oct 28, 2024 · 10 comments
Labels
enhancement New feature or request Service Related to external http, grpc communciation , middlewares etc.

Comments

@shashank-zopsmart
Copy link

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.

@vipul-rawat vipul-rawat added Service Related to external http, grpc communciation , middlewares etc. enhancement New feature or request labels Oct 29, 2024
@rigved-telang
Copy link
Contributor

Please assign this issue to me

@Umang01-hash
Copy link
Contributor

@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?

@rigved-telang
Copy link
Contributor

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.

@Umang01-hash
Copy link
Contributor

@rigved-telang sure you can take your time. Will look forward to discussing with you about implementation

@rigved-telang
Copy link
Contributor

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.

@Umang01-hash
Copy link
Contributor

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.

@Umang01-hash
Copy link
Contributor

@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.

@rigved-telang
Copy link
Contributor

Cool, I'll look into this and let you know. Thanks

@coolwednesday
Copy link
Contributor

@rigved-telang , Any updates on this discussion ?

@rigved-telang
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Service Related to external http, grpc communciation , middlewares etc.
Projects
None yet
Development

No branches or pull requests

5 participants