# Mopidy Websocket Connection with Flask

**URL:** <https://discourse.mopidy.com/t/mopidy-websocket-connection-with-flask/4181>\
**Category:** Misc\
**Created:** [July 19, 2020, 9:54am UTC](https://discourse.mopidy.com/t/mopidy-websocket-connection-with-flask/4181 "2020-07-19T09:54:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nik661](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nik661](https://discourse.mopidy.com/u/nik661)\
**Post date:** [July 19, 2020, 9:54am UTC](https://discourse.mopidy.com/t/mopidy-websocket-connection-with-flask/4181/1 "2020-07-19T09:54:14Z")

</div>

Hi all,  
I’m trying to create my own simple mopidy web client using the websocket API.  
So far I’m running a flask server with a python script which already uses [socket.io](http://socket.io) as a websocket for general purpose. I now want to simply connect from within this python script to ws://localhost:6680/mopidy/ws to send and receive messages.  
Since I am relatively new to webdevelopment the documentation is not sufficient for me. I even tried the module “websockets” instead of “[socket.io](http://socket.io)” but unfortunately without success too.  
Does anyone have a simple example on how to connect to mopidy ws interface through a python script?

---

<div class="post-metadata">

**Author:** ![nik661](https://avatars.discourse-cdn.com/v4/letter/n/e47c2d/32.png) [@nik661](https://discourse.mopidy.com/u/nik661)\
**Post date:** [July 19, 2020, 10:05am UTC](https://discourse.mopidy.com/t/mopidy-websocket-connection-with-flask/4181/2 "2020-07-19T10:05:42Z")

</div>

For example I tried following code within my python script:

> import asyncio  
> import websockets
> 
> async def test():  
> uri = “ws://localhost:6680/mopidy/ws”  
> async with websockets.connect(uri) as websocket:  
> await websocket.send(‘{“jsonrpc”: “2.0”, “id”: 1, “method”: “core.get\_version()”}’)  
> await websocket.recv()  
> asyncio.get\_event\_loop().run\_until\_complete(test())

This at least does connect to the mopidy websocket server because if the server if offline I get a error message from Flask webserver. However, if its online and probably connected, how do I send and receive messages?

got that code from [websocket documentation](https://websockets.readthedocs.io/en/stable/intro.html)
