Websocket is not working - But HTTP upgrade is working

Hi All,
we are trying the connect the “Kong API Gateway” which is running at “localhost:8000” for public, we are able host the API and able to get the data from upstream system.

But when we are trying the access the same API using “ws://localhost:8000” url then we are getting the

WebSocket connection to ‘ws://localhost:8000/getCustomerInfo’ failed: Error during WebSocket handshake: Unexpected response code: 404

Web-socket Code is:

<script>
var ws = null;
function connect() {
  if (ws !== null) return log('already connected');
  ws = new WebSocket('ws://localhost:8000/getCustomerInfo');
  ws.onopen = function () {
    log('connected');
  };
  ws.onerror = function (error) {
    log(error);
  };
  ws.onmessage = function (e) {
    log('recv: ' + e.data);
  };
  ws.onclose = function () {
    log('disconnected');
    ws = null;
  };
  return false;
}
function disconnect() {
  if (ws === null) return log('already disconnected');
  ws.close();
  return false;
}
function send() {
  if (ws === null) return log('please connect first');
  var text = document.getElementById('text').value;
  document.getElementById('text').value = "";
  log('send: ' + text);
  ws.send(text);
  return false;
}
function log(text) {
  var li = document.createElement('li');
  li.appendChild(document.createTextNode(text));
  document.getElementById('log').appendChild(li);
  return false;
}
</script>

and we are using the Docker container for running the KONG.
image

Kindly assist what we are doing wrong.

@bungle cab you helps me?

Can you show your route and host configuration? For some reason it is giving you 404. Is it Kong or is it upstream? Can you also show response headers?

Hi ,
Thanks for reply, appreciated.

I had used KONGA for hosting the service…


Service Configuration:

{
“host”: “kong.demo.in”,
“created_at”: 1590562892,
“connect_timeout”: 60000,
“id”: “2aa1841b-375b-4da4-88ac-7af5993f417a”,
“protocol”: “http”,
“name”: “cusomer_demographic”,
“read_timeout”: 60000,
“port”: 8090,
“path”: “/”,
“updated_at”: 1590562892,
“retries”: 5,
“write_timeout”: 60000,
“tags”: [
“Customer”
],
“client_certificate”: null,
“extras”: {
“createdUser”: null,
“updatedUser”: null,
“kong_node_id”: “5ece1019c25d7100063580a7”,
“service_id”: “2aa1841b-375b-4da4-88ac-7af5993f417a”,
“createdAt”: “2020-05-27T07:01:32.726Z”,
“updatedAt”: “2020-05-27T07:01:32.726Z”,
“id”: “5ece104cc25d7100063580a8”
}
}

Routes Configuration

{
“id”: “c76298fb-de0d-4051-a8dc-849b382205fc”,
“path_handling”: “v1”,
“paths”: null,
“destinations”: null,
“headers”: null,
“protocols”: [
“http”
],
“methods”: [
“POST”
],
“snis”: null,
“service”: {
“id”: “2aa1841b-375b-4da4-88ac-7af5993f417a”
},
“name”: null,
“strip_path”: true,
“preserve_host”: false,
“regex_priority”: 0,
“updated_at”: 1591389964,
“sources”: null,
“hosts”: null,
“https_redirect_status_code”: 426,
“tags”: null,
“created_at”: 1590562913
}

and Host Configuration

YES; I had tried as well by removing the HTTP protocol, but got same result.

It seems like you are routing by POST method. I think that WebSocket upgrade request will not send POST but GET.

When we are sending request from POSTMAN using http://localhost:8000/getCustomerInfo/ then i am sending the connection upgrade request in the header —
image

but ws not requested any upgrade (kindly correct me if my understanding is wrong).
I am just using above javascript code to get connect.

However when we set the method as “ws” then we are getting the same error .
image

{
“id”: “f0f69a9c-a438-492f-8650-fe769094a5f9”,
“path_handling”: “v1”,
“paths”: null,
“destinations”: null,
“headers”: null,
“protocols”: [
“http”
],
“methods”: [
“ws”,
],
“snis”: null,
“service”: {
“id”: “ef293a98-8eec-46ca-8687-3dbf3e6ce341”
},
“name”: null,
“strip_path”: true,
“preserve_host”: false,
“regex_priority”: 0,
“updated_at”: 1591767646,
“sources”: null,
“hosts”: null,
“https_redirect_status_code”: 426,
“tags”: null,
“created_at”: 1591733014
}

However i can not set protocol as ws, kong is throwing error.

What is your backend server? I faced a similar issue with a service using Java Spring and had to fork Kong because of the way it handles the upgrade header. Spring was very specific and wasn’t doing the upgrade.

Have got any positive results on this. since i am facing the same issue