Support for websockets

I ended up sketching the implementation for both WebSocket and SocketIO components. I agree with you that the WebSocket approach is more “clear”, though it requires some more code in the Python layer. I have included the WebSocket component in dash-extensions==0.0.39, if anyone wan’t to try it out :slight_smile:

Here is a small example app that writes data to a websocket, reads the response, and displays it,

import dash_core_components as dcc
import dash_html_components as html

from dash import Dash
from dash.dependencies import Input, Output
from dash_extensions import WebSocket

# Create example app.
app = Dash(prevent_initial_callbacks=True)
app.layout = html.Div([
    dcc.Input(id="input", autoComplete="off"), html.Div(id="message"),
    WebSocket(url="wss://echo.websocket.org", id="ws")
])

# Write to websocket.
@app.callback(Output("ws", "send"), [Input("input", "value")])
def send(value):
    return value

# Read from websocket.
@app.callback(Output("message", "children"), [Input("ws", "message")])
def message(message):
    return f"Response from websocket: {message['data']}"  # read from websocket

if __name__ == '__main__':
    app.run_server()

The WebSocket component enables a number of use cases which can otherwise be cumbersome in Dash, e.g.

  • Updating client content without server interaction
  • Pushing updates from the server to the client(s)
  • Running long processes asynchronously
7 Likes

Found this thread, thought this would be useful information.

It’s an asyncio implementation of the dash server using Quart, not Flask. It also support for Websockets, which makes Dash quite a bit faster. There are other implementation speedups possible as well, like speeding up callback chaining.

The roadmap for Dash should really consider including Websockets. Websockets are fully supported across all browsers in 2021.

1 Like

I would like to see this feature be impleneted in Dash as well. Checked GitHub - richlegrand/dash_devices: A fork of plotly/dash to help Dash deal with devices. , it is awasome ! :slight_smile:

Curious whether there have been any updates @Emil on the websocket extension that I had seen you created at one point? I feel like that is an extension that I would get heavy use out of if I knew it were being maintained/developed and would make sense to see an implementation officially hit Dash at some point, as I think there are more than enough uses for having real-time streaming data in Dashboard applications. Thanks! :slight_smile:

@dash-beginner I have recently made updates to the documentation,

but the component itself has not changed. I expect to keep maintaining it (should maintenance be needed). If you encounter any issues with the component itself, please let me know :slight_smile:

Wonderful - appreciate all that you do for the community and look forward to implementing it into my project sometime! :slight_smile: