I'm currently using the websocket-client library to connect to a websocket by following the example code:
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send('{ "event": "subscribe", "channel": "sensor"+i }')
time.sleep(1)
thread.start_new_thread(run, ())
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
however if I try to send additional message to the websocket on-demand (say subscribe additional channels,
ws.send('{ "event": "subscribe", "channel": "sensor5" }')
How would I be able to achieve such? As the ws.run_forever() is already running, how can I get hold of the running websocket instance to submit the message?
It's more of a listener than the sender, for sending you can use this:
from websocket import create_connection
ws = create_connection("ws://echo.websocket.org/")
print("Sending 'Hello, World'...")
ws.send("Hello, World")
For Listener in your question snippet, you had on_message
Of whose implementation you can do:
def on_message(ws, message):
print(message) #do something with the message
Related
I have this...
import websocket
SOCKET = "wss://stream.binance.com:9443/ws/ADABUSB#nav_kline_1m"
def on_open(ws):
print('opened connection')
def on_close(ws):
print('close connection')
def on_message(ws, message):
print('received message')
print(message)
ws = websocket.WebSocketApp(SOCKET, on_open = on_open, on_close = on_close, on_message = on_message)
ws.run_forever()
When I run it it sticks on OPENED CONNECTION and then does nothing??
Any ideas?
No error messages and I have left it for minutes!!
Cheers
Zak
No error messages
How do you know, you haven't defined the on_error-callback :) ?
Try adding it like below and see if it makes a difference (it does on my end):
import websocket
SOCKET = "wss://stream.binance.com:9443/ws/ADABUSB#nav_kline_1m"
def on_open(ws):
print('opened connection')
def on_close(ws):
print('close connection')
def on_message(ws, message):
print('received message')
print(message)
def on_error(ws, message):
print('error:', message)
ws = websocket.WebSocketApp(SOCKET, on_open = on_open, on_close = on_close, on_message = on_message, on_error = on_error)
ws.run_forever()
Found this and it worked!
I am on a Mac...
Spot on! removed ::1 from /etc/hosts and its connecting. localhost was resolving to ::1. Thanks for help.
I am trying to develop a short script that connects to a real-time stock data provider through a websocket API, gets some data, makes some calculations, stores the results in a database and stops.
EDIT: I need to keep the connection alive for a few seconds until I get all required data. Thus, breaking the connection after the first message is not an option.
The problem I am facing is how to stop the run_forever() connection.
This is what I have so far:
import websocket
import json
def on_open(ws):
channel_data = {
"action": "subscribe",
"symbols": "ETH-USD,BTC-USD"
}
ws.send(json.dumps(channel_data))
def on_message(ws, message):
# Do some stuff (store messages for a few seconds)
print(message)
def on_close(ws):
print("Close connection")
socket = "wss://ws.url"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
ws.run_forever()
ws.close()
# Once the connection is closed, continue with the program
I do not want to stay connected after the "Do some stuff" is executed, how can I force the connection close?
Your help is much appreciated.
I managed how to solve this. I leave my solution in case it is useful to someone.
I just added some attributes to the ws object that allows me to track the number of messages received and store them into a list to work with once the connection is closed.
import websocket
import json
def on_open(ws):
channel_data = {
"action": "subscribe",
"symbols": "ETH-USD,BTC-USD"
}
ws.send(json.dumps(channel_data))
def on_message(ws, message):
ws.messages_count+=1
ws.messages_storage.append(message)
if ws.messages_count>50:
ws.close()
def on_close(ws):
print("Close connection")
socket = "wss://ws.url"
ws = websocket.WebSocketApp(socket, on_open=on_open, on_message=on_message)
# Create a counter and an empty list to store messages
ws.messages_count = 0
ws.messages_storage = []
# Run connection
ws.run_forever()
# Close connection
ws.close()
# Continue with the program
I'm trying to wrap my head around the usage of websocket. Here's my code:
import websocket
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print('Websocket: closed')
def on_open(ws):
print('Websocket: open')
ws = websocket.WebSocketApp('ws://echo.websocket.org/',
on_message = on_message,
on_error = on_error,
on_close = on_close,
on_open = on_open)
ws.run_forever()
First, why would I put
ws.on_open = on_open
instead of passing it while defining ws? (As I did in the code)
Moreoever, how can I send a message? I could include
ws.send(json.dumps("Hello")) under
def on_open(ws), is there some other way?
Finally, I'm not able to close the connection given that ws.run_forever() never stops, how can I do it? I tried to include ws.close() under def on_open(ws) but then I don't receive any message.
What I'm trying to get is how I can transpose this:
ws = ws.create_connection('ws://echo.websocket.org/')
ws.send(json.dumps("Hello"))
result = json.loads(ws.recv())
print(result)
using WebSocketApp, that is having the messages pushed printed directly, without having to request the result.
I want to make one real-time chat application using websockets and the frontend is angular5.
So, I create websocket in purepython and backend is Django and frontend is angular5.
Myquestion is when i create websocket service in python. So, do i have to make websockets services in angular too?
this is my python websocket service
async def consumer_handler(websocket):
global glob_message
while True:
message = await websocket.recv()
await glob_message.put(message)
print("this went in glob_message: {}".format(message))
async def producer_handler(websocket):
global glob_message
while True:
message = await glob_message.get()
await websocket.send(message)
async def handler(websocket, path):
producer_task = asyncio.ensure_future(producer_handler(websocket))
consumer_task = asyncio.ensure_future(consumer_handler(websocket))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)
for task in pending:
task.cancel()
if __name__ == '__main__':
glob_message = asyncio.Queue()
start_server = websockets.serve(
handler,
'127.0.0.1', 8788)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
So, i want to create two user can send and receive messages.so must be design in angular ..
so i am asking that i already create one service in python so do i have to create websocket service(Observable subscribers) in angular too?
In your angular side, you should open a connection indeed.
This is how you can use Observables to communicate with your server :
Client side
// open the socket connection
const ws = new WebSocket('ws://127.0.0.1:8788')
// when opened, print all messages
ws.onopen = open => {
Observable.fromEvent(ws, 'message')
.subscribe(message => console.log(message))
}
To send a message, simply use :
ws.send('test')
Server Side
You can use the demo in https://pypi.org/project/websocket-client/ to build your websocket server.
to import WebSocket :
pip install websocket-client
Then :
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://127.0.0.1:8788",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
I have set up websocket connections to multiple cryptocurrency exchanges but I'm having difficulty connecting to bitFlyer's.
My code is as follows:
import websocket
import json
def on_message(ws, message):
msg = json.loads(message)
print(msg)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
ws.send(json.dumps({"method":"subscribe", "channel":"lightning_executions_FX_BTC_JPY"}))
while True:
if __name__ == "__main__":
#websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
I have tried many many variations of my on_open() message and most result in a ### closed ###
Invalid close opcode. error.
Unfortunately their documentation does not contain a Python sample located HERE.
Any help in sending the correct message is much appreciated.
I believe the format of message you sent was wrong, check the following reference from https://lightning.bitflyer.jp/docs/playgroundrealtime, guess it will solve.
# pip install websocket-client
import websocket
import json
CHANNEL = "lightning_board_snapshot_<productCode>"
def on_message(ws, message):
message = json.loads(message)
if message["method"] == "channelMessage":
print("{} {}".format(message["params"]["channel"], message["params"]["message"]))
def on_open(ws):
ws.send(json.dumps({"method": "subscribe", "params": {"channel": CHANNEL}}))
if __name__ == "__main__":
// note: reconnection handling needed.
ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc",
on_message = on_message, on_open = on_open)
ws.run_forever()