question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to access server instance from inside an event handler

See original GitHub issue

` server = SIOServer()

@server.service.on('get_bananas', namespace="/namespace1")
async def some_request_1(sid=None, data=None):
    #server.service.enter_room(sid, f'user__{sid}')
    print(f'EMITING server_response WITH SID {sid} and namespace /namespace1')

    await server.service.emit('server_response', {}, namespace='/namespace1')
    # BUT server IS NOT DEFINED ?????

    #server.service.leave_room(sid, f'user__{sid}')

server.start_service()
async_to_sync(server.service.emit)('server_response', {}, namespace='/namespace1')

`

I’m trying to respond from the server by emitting something inside the event handler. That means client-says-something ----> triggers-event-in-server ----> server-emits-to-all

But I am unable to access the server instance from inside the handler. How can I get access to it ? Is there any special variable ? I’ve been unable to find a suitable example for this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
miguelgrinbergcommented, Sep 17, 2019

The error is on this line:

async_to_sync(sio_client.emit)('get_bananas', {}, namespace='/namespace1')

This is a problem with your client, not your server. You created a sync client, and then are trying to use it as if it was an asyncio client.

0reactions
mrp470commented, Sep 17, 2019

I don’t know how I missed that. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access object instance inside an event handler - Stack Overflow
I have a DOM structure with a couple of <input type="text" /> elements. I need to write a couple of methods to enhance...
Read more >
Event handling (overview) - Event reference - MDN Web Docs
You can use the Event reference to find out what JavaScript objects fire events for particular APIs, e.g. animation, media, and so on....
Read more >
Integration Services (SSIS) Event Handlers - Microsoft Learn
In the Event handler list, select the event handler you want to build. Click the link on the design surface of the Event...
Read more >
Event handling - discord.js Guide
In most cases, you can access your client instance in other files by obtaining it from one of the other discord.js structures, e.g....
Read more >
Handling and dispatching events with Node.js - LogRocket Blog
Learn how to create, dispatch, and manage events in Node.js. ... http, Allows you to create a http server and handle data transfer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found