> ## Content Index
> Fetch the complete content index at: https://whatimade.today/llms.txt
> Use this file to discover other available public pages before exploring further.

# Realtime graphs using Plotly and websockets
- URL: https://whatimade.today/realtime-graphs-using-ploty-and-websockets/
- Published: 2016-06-04T15:39:44.000Z
- Updated: 2016-06-24T13:08:30.000Z
- Author: notamacuser
- Tags: python, javascript, Websocket, Tutorial, web, plotly, #Import 2026-11-07 09:43

As you already know, In our blog we use the [websocket protocol](http://www.whatimade.today/tag/websocket/?ref=whatimade.today) intensively, It's a fast and easy way to send data from WiFi connected micro-controllers (such as the ESP8266) to the Browser without anything in between.

Here are some projects with code samples where websockets were used:

[Spectrum Analyzer](https://whatimade.today/part-2-spectrum-analyzer-with-esp8266-msgeq7-ws2812/)

[Living-room lights](https://whatimade.today/esp8266-on-websockets-mdns-ota-and-leds/)

Today I'll share with you an example of how you can use [Plotly](https://plot.ly/javascript/?ref=whatimade.today) to visualize real-time data in your browser

Here is how it looks like:

![ploty websocket example](https://whatimade.today/content/images/2016/06/ploty_websocket_example.gif)

Here is the code : [ploty websocket example](https://github.com/dannyvai/ploty%5Fwebsocket%5Fexample?ref=whatimade.today)

The HTML code creates a websocket (You'll need to change this to the ESP8266 IP and port)

> ws = new WebSocket("ws://esp\_ip:port/")

After the connection is established, it creates a loop of requesting data from the server

request\_data\_interval = window.setInterval(requestData, 50);

Then, it parses the json data and creates the graph in the place I asked him to

```   
    ws.onmessage = function (evt) 
    { 
        var received_msg = evt.data;

        data = JSON.parse(evt.data);
        var my_plot = {
            x: data.x, 
            y: data.y, 
            type: 'scatter',
        };
        Plotly.newPlot('sine-graph', [my_plot]);
    };
```

In order to test this I've used a python library called [pywebsocket](https://github.com/google/pywebsocket?ref=whatimade.today)

I've created a websocket handler named [send\_graph](https://github.com/dannyvai/plotly%5Fwebsocket%5Fexample/blob/master/send%5Fgraph%5Fwsh.py?ref=whatimade.today)

All it does is create a sine-wave graph with lots of samples

```
x = np.arange(0,np.pi*10,0.1).tolist()
y = np.sin(x).tolist()
```

And every time the user requests the graph data we send a sliding window of the graph

```
    graph_to_send = json.dumps({
        'x':x[counter:counter+graph_size],
        'y':y[counter:counter+graph_size]
    })
```

and that's it :)

In order to try the `pywebsocket` library for yourself you should download it and install using your **python**. Then, copy the `send_graph_wsh.py` file in the repository to the `pywebsocket/example/` folder and then run the next lines:

```
cd pywebsocket/mod_pywebsocket
python standalone.py -p 9998 -w ../example/ 

```

If you open the `plot_graphs_from_websocket.html` using your browser you'll see the results - and that's it :)