Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

this is not even working at all #22

Closed
ahmetkca opened this issue Aug 15, 2021 · 5 comments
Closed

this is not even working at all #22

ahmetkca opened this issue Aug 15, 2021 · 5 comments

Comments

@ahmetkca
Copy link

ahmetkca commented Aug 15, 2021

main.py

from fastapi import FastAPI
from fastapi_socketio import SocketManager

app = FastAPI()
socket_manager = SocketManager(app=app)


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.sio.on('connect')
async def handle_connect(sid, *args, **kwargs):
    await app.sio.emit('connect', 'User joined')

CLIENT SIDE

import {io} from 'socket.io-client';

const socket =  io('ws://127.0.0.1:8000', {
      path: 'ws/socket.io',
      autoConnect: true
  })

  socket.on('connect', (data) => {
    console.log(data)
  })``` 
@jpez16
Copy link

jpez16 commented Sep 12, 2021

check your ports, are you using docker?

@jujaryu
Copy link

jujaryu commented Sep 18, 2021

you have typo in path string, change

path: 'ws/socket.io'
to
path: '/ws/socket.io'

@dustinmichels
Copy link

Setting path to '/ws/socket.io seems to have worked for me...

I would really appreciate a little more documentation on how to connect to the server!

@cande1gut
Copy link

Whoever ends up here and is trying within a python client, you can establish a connection with:

import socketio

sio = socketio.Client()

@sio.on('connect_server')
def connected(message):
    print(message)

sio.connect('http://{ip}{port}/ws', socketio_path="/ws/socket.io", wait_timeout = 10)
sio.wait()

@ahmetkca
Copy link
Author

ahmetkca commented Oct 21, 2021

I ended up using socketio itself;

MySocketManager.py

class MySocketManager:
	def __init__(self, app: FastAPI) -> None:
		self._mgr = socketio.AsyncRedisManager(REDIS_TLS_URL)
		self._sio: AsyncServer = socketio.AsyncServer(
			client_manager=self._mgr, 
			async_mode=ASYNC_MODE, 
			cors_allowed_origins=[]
		)
		self._app = socketio.ASGIApp(
			socketio_server=self._sio,
			socketio_path=SOCKETIO_PATH
		)
		app.mount(MOUNT_LOCATION, self._app)
		app.sio = self._sio
		logging.info

	def get_socket_manager(self) -> AsyncServer:
		return self._sio

app.py

from MySocketManager import MySocketManager as SocketManager

app = FastAPI()
socketio_manager = SocketManager(app)
sm: AsyncServer = socketio_manager.get_socket_manager()
connected_users = set()

@sm.on('connect')
async def on_connect(sid, *args, **kwargs):
	print('a user connected ', sid)


@sm.on('disconnect')
async def on_disconnect(sid, *args, **kwargs):
	sm.leave_room(sid, room='orderaio')
	ss = await sm.get_session(sid)
	connected_users.remove(ss['username'])
	print('a user disconnected ', sid)

and make sure when you establish a connection from client use "/ws/socket.io/" as your path.

@pyropy pyropy closed this as completed Nov 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants