Twitch IRC Returning Empty String


I’m using Python to grab chat messages. Eventually, after some time, the IRC seems to start returning empty strings and doesn’t revert to sending actual messages. When I restart my program, it’ll go back to working without issue for some time, until it inevitable happens again.

Is this what happens when a program sends too many requests to the IRC server? I’m thinking no, since it starts working again as soon as I restart the program, but I can’t think of what else it could be, due to it working initially, but then inevitably breaking.

Sounds like you might not be responding to PING’s with PONG’s and you are getting DC’ed

But your script doesn’t realise it got DC’ed and sits in a recieve nothing loop.

Here is essentially what my code is, besides connection variables

sock = socket.socket()
    sock.connect((server, port))
    sock.send(f"PASS {token}\r\n".encode('utf-8'))
    sock.send(f"NICK {nickname}\r\n".encode('utf-8'))
    sock.send(f"JOIN {channel}\r\n".encode('utf-8'))

    while True:
        resp = sock.recv(2048).decode('utf-8', 'ignore')
        if resp.startswith('PING'):
        messageList = resp.splitlines()
        time.sleep(rate + 1)
        if not resp.startswith('') and not resp.startswith(':streamelements') and not resp.startswith(':fossabot'):
            if resp == "":
                print('---------------EMPTY STRING---------------')
            for chatMessage in messageList:
                if chatMessage.find(' :') != -1 and chatMessage.find('!') != -1 and chatMessage.find('@') != -1:
                    userWithThing = chatMessage.split('!')[0]
                    user = userWithThing[1:]
                    message = chatMessage.split(' :')[1]
                    formattedChannel = channel[1:]
                    print('User: ' + user + ' Message: ' + message)

Am I handling the Pings correctly? I do see instances where “PINGPONG” gets printed to console, but they happen pretty irregularly.

Seems correct.

Personally if Twitch doesn’t PING withing a given time frame I’ll send my own.

But your original problem seesm to suggest something isn’t write with the ping/pong/disconnect logic

Or you can’t handle extended periods of “no messages sent to a channel” for you to read nicely

The problem with the disconnect logic in that code is that there is none. When you do a blocking receive like that, getting no data back from the recv() call means that the connection has been dropped by the other end. The official documentation for Python’s socket library doesn’t seem to mention it, but the info is out there.

Also, with TCP, there’s no guarantee that what you get from a given receive call is going to contain neatly grouped protocol messages, especially when you start getting a lot of them coming in. What I generally do is append the raw data coming in to a working buffer (not really a Python developer, so I don’t know how you’d do that), search that buffer for the message delimiter (\n in this case), and separate the messages myself. If I have data left over but can’t find a message delimiter in it, I keep that in its raw state and wait for more to come in.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.