How do I use my droplet as a server using the python websockets module?

Posted April 15, 2020 883 views

I’m a complete beginner to network programming. I’m writing a program to allow me to play board games with my friends throughout isolation (I know, I know, there are already similar programs available online, but I want to make my own to make myself a better programmer). I’m trying to use the websockets library on python to allow me and my friends to connect to the droplet I’ve set up. When my client tried to connect to the server I get the following error:

RuntimeError                              Traceback (most recent call last)
~\Desktop\test_program\ in <module>
---> 25 asyncio.get_event_loop().run_until_complete(communicate())

~\AppData\Local\Continuum\anaconda3\lib\asyncio\ in run_until_complete(self, future)
    558         future.add_done_callback(_run_until_complete_cb)
    559         try:
--> 560             self.run_forever()
    561         except:
    562             if new_task and future.done() and not future.cancelled():

~\AppData\Local\Continuum\anaconda3\lib\asyncio\ in run_forever(self)
    513         self._check_closed()
    514         if self.is_running():
--> 515             raise RuntimeError('This event loop is already running')
    516         if events._get_running_loop() is not None:
    517             raise RuntimeError(

RuntimeError: This event loop is already running

Does anyone know what I might be doing incorrectly? Could it be an issue with the firewall on my computer or on my droplet?


edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi there @robertwdg,

It does not look like a firewall problem. I can see that there was a similar problem reported here and what fixed the problem for the user was installing nest_asyncio:

  • pip install nest_asyncio

And adding the following lines:

import nest_asyncio

Let me know how it goes!

  • Hi @bobbyiliev,

    Thank you so much for your help. No luck just yet. I’m getting a timeout error now (TimeoutError: [Errno 10060] Connect call failed).

    I don’t yet have a good understanding of the asyncio module which might be part of my problem. I’ll let you know if I have any luck.

    Thanks again! I’m new to this, so I really appreciate the help.


    • Hi there @robertwdg,

      No problem at all! This sounds like a great project! Good luck with this and let me know how it goes!


      • Hi Again @bobbyiliev,

        A rudimentary version of my game is up and running! There’s a long lag in sending and receiving messages and I’ve got a bit of fine tuning of the game mechanics but I’m really happy to have it running given that I had never done any networking before this project.
        Once again, I really appreciate your help.


        • Hi @robertwdg,

          That is amazing! Great job and well done! I am really happy to hear that you’ve got it all up and running.

          Feel free to share a link to the game once you release it for everyone, I would love to give it a try!