ballerina-platform / ballerina-library

The Ballerina Library
https://ballerina.io/learn/api-docs/ballerina/
Apache License 2.0
137 stars 61 forks source link

Connect to a websocket with http listener is not working #4764

Open SasinduDilshara opened 1 year ago

SasinduDilshara commented 1 year ago

I tried to initiate a websocket connection with following program.

import ballerina/http;
import ballerina/graphql;
import ballerina/websocket;

listener http:Listener 'listener = check new(9090);
listener graphql:Listener graphqlListener = new('listener);
listener websocket:Listener wsListener = new('listener);

service /ws on wsListener {
    resource function get .(string id, string 'type) returns websocket:Service {
        if 'type == ORDER_TYPE {
            ...
        } else {
            ...
        }
    }
}

This not worked properly. I got the following response

Status Code: 500 Internal Server Error
Request Headers
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: PWM4ryWB6hBgeVhJbUyEvA==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: localhost:9090
ThisaruGuruge commented 1 year ago

This probably happens due to the HTTP version mismatch. WebSockets support the HTTP 1.1 version. In Ballerina, the default HTTP version is 2.0 (in the ballerina/http module). Change the HTTP version of the listener and this should work.

However, I think ideally it should be handled from the WebSocket module itself.

listener http:Listener 'listener = check new(9090, {
    httpVersion: http:HTTP_1_1
});
t-trafer commented 1 year ago

hi, i would like to work on this issue

ThisaruGuruge commented 12 months ago

👋 Welcome, @t-trafer! 🚀

We're thrilled to have you join the Ballerina Lang community! Whether you're a seasoned developer or just starting your journey with Ballerina, we value your contributions and look forward to collaborating with you. To help you get started, here are some essential resources:

  1. Understanding Ballerina Platform: Learn what Ballerina is all about: Ballerina Platform
  2. How to Contribute: Read our contributing guidelines to understand how you can contribute effectively: Contribution Guide
  3. Contributing to the Ballerina library: If you're interested in contributing to our library, here are the guidelines: Library Contribution Guide
  4. Learn Ballerina: If you're new to Ballerina or want to enhance your skills, our official website offers a wealth of learning resources: Learn Ballerina
  5. Get Help and Connect: Join our Discord community to chat with fellow Ballerina enthusiasts and get assistance: Ballerina Discord

Remember, no contribution is too small, and your feedback is invaluable. Feel free to ask questions, propose ideas, or report issues. Together, we can make Ballerina even better! Happy coding! 🎉

ThisaruGuruge commented 12 months ago

hi, i would like to work on this issue

I have assigned the issue for you.

To give you some context,

This error is occurring due to the Ballerina HTTP module using HTTP 2.0 by default, but the WebSocket needs HTTP 1.1. Therefore, when you pass an HTTP listener with default configs, the WebSocket listener initialization fails.

There are two approaches of handling this:

  1. Return an error if the HTTP listener has non compatible HTTP version
  2. Forcefully change the HTTP version to a compatible one inside the WebSocket listener initialization.

@Bhashinee any thoughts on this?

Bhashinee commented 12 months ago

I prefer the first one. With the second approach, we are internally doing manipulations that are not visible to the user. So when this listener is used with another HTTP service, the user might think it is running as an HTTP/2 listener even though we have changed that internally to use HTTP/1.1. With the first approach, the user becomes aware of that change as he is the one who has to configure it in the listener.

ThisaruGuruge commented 12 months ago

I prefer the first one. With the second approach, we are internally doing manipulations that are not visible to the user. So when this listener is used with another HTTP service, the user might think it is running as an HTTP/2 listener even though we have changed that internally to use HTTP/1.1. With the first approach, the user becomes aware of that change as he is the one who has to configure it in the listener.

+1

t-trafer commented 12 months ago

Thanks for the heads up and suggestions! going ahead with the first approach

keizer619 commented 10 months ago

@t-trafer Are you working on this?

m-tabish commented 6 days ago

Kindly assign this to me, under Hacktoberfest 2024. I will try to solve this

ThisaruGuruge commented 6 days ago

Kindly assign this to me, under Hacktoberfest 2024. I will try to solve this

Thank you for participating in Hacktoberfest with Ballerina. I have assigned the issue to you now.