Skip to content

Conversation

@l4mby
Copy link
Contributor

@l4mby l4mby commented Jul 17, 2025

In this PR we add support for websockets, we also add an example.
NOTE: at the moment we don't have a rabbitmq image which supports websockets so we cannot test this directly.
This part is still highly WIP.

To run the example, simply:

  • clone or download the repo:
  • setup your rabbitmq image
  • open examples/websocket_example.js and modify the rabbit credentials
  • enter the examples folder and run npm install, npm run rebuild-source and npm run websocket-example

As for the new feature, the new code looks like this:

const connection = createEnvironment({
    host: rabbitHost,
    port: rabbitPort,
    username: rabbitUser,
    password: rabbitPassword,
    webSocket: WebSocket,  // here goes the WebSocket interface, we used the node built-in one but you can also use the one from the **ws** npm package
  })

@l4mby l4mby force-pushed the 57-as-a-library-user-i-want-to-use-the-client-within-the-browser-also-using-websockets branch from 76127f7 to fe68a33 Compare July 17, 2025 09:08
@l4mby l4mby merged commit b535922 into main Jul 17, 2025
4 checks passed
@apietroni51 apietroni51 deleted the 57-as-a-library-user-i-want-to-use-the-client-within-the-browser-also-using-websockets branch July 17, 2025 09:14
@ansd
Copy link
Collaborator

ansd commented Jul 17, 2025

Thank you very much!

The following patch on top of this PR works for me:

diff --git a/examples/websocket_example.js b/examples/websocket_example.js
index 1283e64..fbd1f25 100644
--- a/examples/websocket_example.js
+++ b/examples/websocket_example.js
@@ -1,10 +1,10 @@
 const rabbit = require("rabbitmq-amqp-js-client")
 const { randomUUID } = require("crypto")

-const rabbitUser = process.env.RABBITMQ_USER ?? "rabbit"
-const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "rabbit"
+const rabbitUser = process.env.RABBITMQ_USER ?? "guest"
+const rabbitPassword = process.env.RABBITMQ_PASSWORD ?? "guest"
 const rabbitHost = process.env.RABBITMQ_HOSTNAME ?? "localhost"
-const rabbitPort = process.env.RABBITMQ_PORT ?? 5672
+const rabbitPort = process.env.RABBITMQ_PORT ?? 15678

 async function main() {
   const testExchange = `test-exchange-${randomUUID()}`
diff --git a/src/connection.ts b/src/connection.ts
index 0a128ea..e7f1568 100644
--- a/src/connection.ts
+++ b/src/connection.ts
@@ -107,11 +107,9 @@ function buildConnectParams(envParams: EnvironmentParams, connParams?: Connectio
   const reconnectParams = buildReconnectParams(connParams)
   if (envParams.webSocket) {
     const ws = websocket_connect(envParams.webSocket)
-    const connectionDetails = ws(
-      `ws://${envParams.username}:${envParams.password}@${envParams.host}:${envParams.port}`,
-      "amqp",
-      {}
-    )
+    const wsUrl = `ws://${envParams.host}:${envParams.port}/ws`
+    const connectionDetails = ws(wsUrl, ["amqp"], {})
+
     return {
       connection_details: () => {
         return {
@@ -120,9 +118,8 @@ function buildConnectParams(envParams: EnvironmentParams, connParams?: Connectio
           port: envParams.port,
         }
       },
-      host: envParams.host,
-      port: envParams.port,
-      transport: "tcp",
+      ...envParams,
+      ...reconnectParams,
     }
   }
  • I think the user name and password should both be guest by default since these are the default credentials.
  • 15678 should be the default port for AMQP over WebSocket in RabbitMQ. 5672 is used for AMQP over plain TCP/SSL.
  • The URL needs to add the /ws path by default, this bit should be configurable ideally.
  • This patch also ensures that SASL is being used during connection set up. Without this patch, it seems that the SASL layer is skipped. However, RabbitMQ require SASL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

As a library user I want to use the client within the browser, also using websockets

4 participants