 <p>Well the error is quite logical</p> <pre><code>if command == "iam": self.name = content msg = self.name + "has joined" elif command == "msg": msg = self.name + ": " + content print msg </code></pre> <p>In the first if clause you assign a value to self.name which may either rely on assumption that self.name exists somewhere, or on assumption that it is new and needs to be declared, but in elif you seem to assume with certainty that self.name already exists, it turns out it doesn't so you get an error.</p> <p>I guess your safest option consists of simply adding self.name at the beginning of dataReceived method:</p> <pre><code>def dataReceived(self, data): self.name = "" </code></pre> <p>this will get rid of the error. As an alternative you could also add self.name to init method of IphoneChat.If you need self.name in other functions not only in dataReceived then adding init with self.name is the way to go, but from your code it seems you only need it in datareceived so just add it there. Adding self.name in init would look like this:</p> <pre><code>class IphoneChat(Protocol): def __init__(self): self.name = "" </code></pre> <p>or you can also simply do </p> <pre><code>class IphoneChat(Protocol): name = "" </code></pre> <p>and then go on with name instead of self.name. </p>