How to establish websocket connection

I am using a SIP provider. There is no problem with that. My ARI connection is also good. I can call myself through it. But I can not establish websocket connection with my python application properly. This is the error I got once I run python application.

Handshake status 400 Bad Request

And also this is the error message I see through asterisk terminal once I run my application.

res_http_websocket.c:866 __ast_websocket_uri_cb: WebSocket connection from '127.0.0.1:53664' could not be accepted - no protocols out of 'ast.websocket.json' supported

This is how I start websocket connection in my appliacation

    def start_websocket(self):
        def on_message(ws, message):
            self.handle_websocket_message(message)

        def on_error(ws, error):
            print(f"❌ WebSocket Error: {error}")

        def on_close(ws, close_status_code, close_msg):
            print("🔴 WebSocket connection closed")

        def on_open(ws):
            print("✅ WebSocket connection started")
            subscribe_msg = {
                "action": "subscribe",
                "eventSource": f"applications:{STASIS_APP_NAME}"
            }
            ws.send(json.dumps(subscribe_msg))
            print(f"Subscribed to application: {STASIS_APP_NAME}")

        userpass = f"{USERNAME}:{PASSWORD}"
        auth = base64.b64encode(userpass.encode()).decode()
        headers = [
            f"Authorization: Basic {auth}"
        ]

        subprotocols = ["ast.websocket.json"]

        self.ws = websocket.WebSocketApp(
            WS_URL,
            header=headers,
            on_message=on_message,
            on_error=on_error,
            on_close=on_close,
            on_open=on_open,
            subprotocols=subprotocols
        )

        # Start WebSocket on another thread
        self.ws_thread = threading.Thread(target=self.ws.run_forever)
        self.ws_thread.daemon = True
        self.ws_thread.start()

        time.sleep(2)
        return True

It means what it says. “ast.websocket.json” is not the protocol to use. Where did you get this from?

The correct protocol is “ari”.

It was AI advice. I just change it with “ari”. but still got same message.

This is also my http.conf

[general]
servername=Asterisk
enabled=yes
bindaddr=0.0.0.0
bindport=8088
;prefix=asterisk
enablestatic=yes

And ari.conf

[general]
enabled = yes
pretty = yes
websocket_write_timeout = 100
allowed_origins = *

[netgsm_user]
type=user
read_only=no
password=123456
password_format=plain

Show it.

res_http_websocket.c:866 __ast_websocket_uri_cb: WebSocket connection from '127.0.0.1:35128' could not be accepted - no protocols out of 'ari' supported

Is the res_ari_events module loaded in Asterisk?

Yes. res_ari_events.so is loaded and running right now.

Are you connecting to the correct URL?

http://hostname:8088/ari/events

No that is not the url I am connection with for websocket. This is the url I am using: ws://localhost:8088/ws

That URL would not work. You must use the one I specified.

I changed websocket url with the url you gave. But got another error. Here:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.8/dist-packages/websocket/_app.py", line 612, in run_forever
    ping_timeout, dispatcher, parse_url(self.url)[3]
  File "/usr/local/lib/python3.8/dist-packages/websocket/_url.py", line 63, in parse_url
    raise ValueError("scheme %s is invalid" % scheme)
ValueError: scheme http is invalid

Sorry, I should have put “ws” as the URL scheme. Force of habit to always do http or https.

ws://localhost:8088/ari/events

Okay now I do not get warning message in the asterisk terminal about protocol. But I still get websocket connection error. Error:

Handshake status 400 Bad Request -+-+- {'server': 'Asterisk', 'date': 'Mon, 16 Jun 2025 09:39:58 GMT', 'cache-control': 'no-cache, no-store', 'conte nt-type': 'text/html', 'content-length': '233'} -+-+- b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\r\n<html><head>\r\n<title>400 Bad Request </title>\r\n</head><body>\r\n<h1>Bad Request</h1>\r\n<p>HTTP request is missing param: [app]</p>\r\n<hr />\r\n<address>Asterisk</address>\r\n</body> </html>\r\n' - goodbye

It says: HTTP request is missing param: [app]

Yes. You need to specify the name of the ARI application you are connecting as, as a query parameter.

Thank you so much it worked! Websocket connected. I was struggling with it for about three days.