Can't create a stable connection by using websockets

What is the problem?

I am trying to develop a client to communicate with the Octoprint server. I want to receive the message from the server when I send a Gcode command by using websocket.

As shown in Octoprint server's terminal, there is a new connection from client. However, it closes immediately so that I can't send any message further.

2022-09-29 10:38:06,315 - octoprint.server.util.sockjs - INFO - New connection from client: ::1 2022-09-29 10:38:06,320 - octoprint.server.util.sockjs - INFO - Client connection closed: ::1

What did you already try to solve it?

  1. I tried the Octoprint python client here, but it didn't work.
  2. I guess that maybe I need to authorize it. How can I realize it by using the API key.

Have you tried running in safe mode?

Yes

Did running in safe mode solve the problem?

No

Systeminfo Bundle

You can download this in OctoPrint's System Information dialog ... no bundle, no support!)

Additional information about your setup

OctoPrint version, OctoPi version, printer, firmware, browser, operating system, ... as much data as possible

OctoPrint version: 1.8.4
Python version: 3.8.8
Printer: Prusa i3 MK3S
Operating system: Win10

You do need to send an authorization message to start receiving messages.

You can see the messages that can be sent and what to send in the documentation here;

https://docs.octoprint.org/en/master/api/push.html

First, you have to POST /api/login, with the API key & the passive flag (exact details of this are in the API docs). Then you will receive a session key which you can send to the socket to authenticate there.

Hi, Thanks for your reply.

I did receive the authenticate message, like {'auth': '_api:2A97885B4F90490AA28E1C0BCD002310'}. Then I tried to send it to the socket, like ["{\"auth\": \"_api:2A97885B4F90490AA28E1C0BCD002310\"}"]

Here is the code.

auth_message = self.authenticate() 
payload = '["' + json.dumps(auth_message).replace('"', '\\"') + '"]'
socket = websocket.WebSocket()
socket.connect(_base_url)    
socket.send(payload) 

However, in the server's terminal, the connection still closed immediately.

2022-09-29 16:35:25,750 - octoprint.server.util.flask - INFO - Passively logging in user _api from ::1
2022-09-29 16:35:25,750 - octoprint.access.users - INFO - Logged in user: _api
2022-09-29 16:35:25,834 - octoprint.server.util.sockjs - INFO - New connection from client: ::1
2022-09-29 16:35:25,835 - octoprint.server.util.sockjs - INFO - User _api logged in on the socket from client ::1
2022-09-29 16:35:25,840 - octoprint.server.util.sockjs - INFO - Client connection closed: ::1`

The socket isn't a raw websocket, it speaks sockjs. So you have to encode the messages first. It's a bit messy (and due to the fact that websockets for a long time weren't as well supported as they are now, hence sockjs was chosen since it supports jsonp and other types of fallbacks), but for now it's the way things are.

Take a look at the included socket client here for how the messages need to be structured, or take a look at the sockjs docs here.

1 Like

Hi, Thank you very much.

Actually I tried the included python socket client here.

import octoprint_client

host = '127.0.0.1'
port = 5000
apikey = myapikey
octoprintClient = octoprint_client.Client(octoprint_client.build_base_url(https=None, httpuser=None, httppass=None, host=host, port=port, prefix=None), apikey)

#Get session key
response = octoprintClient.post("/api/login", {"passive": True})
response.raise_for_status()
data = response.json()
auth_message = {"auth": "{name}:{session}".format(**data)}

#Authenticate
soc = client.create_socket()
soc.auth(auth_message["auth"].split(':')[0], auth_message["auth"].split(':')[1])
#soc.send(auth_message)

However, I got a similar error in the server's terminal.

2022-10-03 23:42:44,331 - octoprint.access.users - INFO - Logged in user: _api
2022-10-03 23:42:44,392 - octoprint.server.util.sockjs - INFO - New connection from client: 127.0.0.1
2022-10-03 23:42:45,386 - octoprint.server.util.sockjs - INFO - User _api logged in on the socket from client 127.0.0.1
2022-10-03 23:42:45,396 - octoprint.server.util.sockjs - INFO - Client connection closed: 127.0.0.1

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.