Skip to content
Advertisement

Hello world with Tornado and Javascript client

I would like to establish a simple two-way communication between a Tornado server and a Javascript client.

When I run the server, followed by opening the page, the server prints out “get” as expected. However, while the server sends something back, the onmessage event never seems to be triggered in the client. Moreover, the Javascript console produces the following error:

JavaScript

I have no clue what I am doing wrong here.

Python server (tornadoserver.py):

JavaScript

Javascript client (tornado.html):

JavaScript

Advertisement

Answer

You need a separate WebSocketHandler instance mapped to a separate URL in your application. So your “tornado.html” should be mapped to the URL “/”, and your Javascript should connect to a WebSocketHandler listening on a URL like “/websocket”. Try this:

JavaScript
Advertisement