0
import os
import sys

from django.core.wsgi import get_wsgi_application

import socketio

sio = socketio.Client()
print('Created socketio client')

@sio.event
def connect():
    print('connected to server')

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect('http://localhost:8000/socket.io/')
sio.wait()

app_path = os.path.abspath(os.path.join(
    os.path.dirname(os.path.abspath(__file__)), os.pardir))
sys.path.append(os.path.join(app_path, 'myapp'))
if os.environ.get('DJANGO_SETTINGS_MODULE') == 'config.settings.production':
    from raven.contrib.django.raven_compat.middleware.wsgi import Sentry
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")

application = get_wsgi_application()
if os.environ.get('DJANGO_SETTINGS_MODULE') == 'config.settings.production':
    application = application

Here is my wsgi.py file, I am not sure whether to use it here or not. When I start the server it throws

File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\socketio\client.py", line 246, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from
socketio.exceptions.ConnectionError: Connection refused by the server

Am I missing out something else?

UPDATE 1

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001E926004C18>
Traceback (most recent call last):
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 140, in inner_run
    handler = self.get_handler(*args, **options)
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\staticfiles\management\commands\runserver.py", line 27, in get_handler
    handler = super().get_handler(*args, **options)
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 65, in get_handler
    return get_internal_wsgi_application()
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 44, in get_internal_wsgi_application
    return import_string(app_path)
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\Programming\python\myapp\config\wsgi.py", line 34, in <module>
    sio.connect('http://localhost:8000/socket.io/')
  File "C:\Users\prabh\AppData\Local\Programs\Python\Python37\lib\site-packages\socketio\client.py", line 246, in connect
    six.raise_from(exceptions.ConnectionError(exc.args[0]), None)
  File "<string>", line 3, in raise_from

UPDATE 2

socket.on('connect', function() {
            console.log('CONNECTED');
            socket.emit('on message', 'data');
        });
Prabhakaran
  • 3,900
  • 15
  • 46
  • 113
  • Have you looked at your Socket.IO server logs? The error clearly states that your Socket.IO server is refusing the connection, so the first place you should look for clues is in the server to find out why it is rejecting. Also, `sio.wait()` here is not really what you want, as it will block your Django application. – Miguel Grinberg Aug 12 '22 at 10:36
  • @MiguelGrinberg Okay am I right using `sio.connect('http://localhost:8000/socket.io/')` in python itself – Prabhakaran Aug 12 '22 at 12:20
  • @MiguelGrinberg I am using runserver, I don't see much details about it. Also I'll attach the log in the question – Prabhakaran Aug 12 '22 at 12:23
  • I think we are not talking about the same thing. You are using a Socket.IO client and connecting to a Socket.IO server. Where is the Socket.IO server in all of this? – Miguel Grinberg Aug 12 '22 at 14:13
  • All these are in Python application. And I am trying to emit from reactjs. I have updated the question with client side code – Prabhakaran Aug 12 '22 at 14:19
  • You are not answering my question. Where is your Socket.IO server? None of the information you put in your question suggests you have a Socket.IO server running. – Miguel Grinberg Aug 12 '22 at 15:33
  • Then I guess I am not doing it. No I haven't made any configuration to run socket.io. Any reference on how to do it? – Prabhakaran Aug 12 '22 at 15:44
  • I guess I need to follow this https://python-socketio.readthedocs.io/en/latest/server.html#installation – Prabhakaran Aug 12 '22 at 15:49
  • I'm really confused, I don't think I can help because you haven't explained well what you want to do. You added a Socket.IO CLIENT to your Django application. Why did you do that? Now it seems you are not intending to connect to a Socket.IO SERVER, so why did you add the CLIENT in the first place? – Miguel Grinberg Aug 13 '22 at 15:54
  • @MiguelGrinberg The thing I am trying to achieve is, it is like peer to peer messaging. When one user send message the other user should receive the notification. I Usually do it through Nodejs and it is quite different in Python – Prabhakaran Aug 31 '22 at 04:23
  • Socket.IO is not peer-to-peer, all communications between clients have to pass through the server. – Miguel Grinberg Aug 31 '22 at 13:26

0 Answers0