Question
Hi all,
I encountered a problem as I want to send data by means of the websocket in two different object
If I try to send data in each object like the following demo codes, I found that each of sending will build new connection, i.e. server get several connection rather than just one connection, which is not my design in my project.
The very simple idea for me to do that is getting the actual websocket object which is embedded in the package and directly conduct sending and receiving, say ws.send("information")
or ws.revc("information")
.
However, I had no idea how to do that.
Can anyone kindly give me some suggestions to that job?
Thank you all!
Demo Codes
import dash
from dash import html, no_update
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
# Websocket connection
from dash_extensions import WebSocket
import json as js
# Declare the application
app = dash.Dash("Test")
# Declare objects
## localhost:5555 is just an example for the url to server
object1 = html.Div([dbc.Button("Send Hello", id="b1"),
WebSocket(url="ws://localhost:5555", id="ws1")],
)
object2 = html.Div([dbc.Button("Send Hello", id="b2"),
WebSocket(url="ws://localhost:5555", id="ws2")],
)
# Send hello function
def send_hello(n_click):
if n_click > 0:
return js.dumps("Hello"), 0
else:
return no_update, no_update
app.callback([Output("ws1", "send"),
Output("b1", "n_clicks")],
Input("b1", "n_clicks")
)(send_hello)
app.callback([Output("ws2", "send"),
Output("b2", "n_clicks")],
Input("b2", "n_clicks")
)(send_hello)