Realtime chat is virtually any online communication that provides a realtime or live transmission of text messages from sender to receiver. This tutorial will show you how to build a realtime terminal chat using Python and Pusher Channels.
It’s lightweight to use the terminal for our chat, as there is no opening of the browser, loading of JS libraries or any frontend code. Also, it allows us to quickly test our ideas without worrying about what the user interface would look like.
Python in this tutorial refers to Python 3.x
A basic understanding of Python is needed to follow this tutorial. You also need to have Python 3 and pip installed and configured on your machine.
Pusher Channels is a hosted service that makes it super-easy to add realtime data and functionality to web and mobile applications.
Pusher Channels acts as a realtime layer between your servers and clients. It maintains persistent connections to the clients over WebSocket if possible and falling back to HTTP-based connectivity. As soon as your servers have new data they want to push to the clients they can do, via Pusher.
To get started with Pusher Channels, sign up. Then go to the dashboard and create a Channels app. The only compulsory options are the app name and cluster. A cluster represents the physical location of the Pusher server that will handle your app’s requests. Also, copy out your App ID, Key, and Secret from the App Keys section, as we will need them later on.
First, we need to install a package called virtualenv
. Virtualenv helps to manage environments in Python. This is so we do not end up with conflicting libraries due to install operations from project to project. To install Virtualenv, we run:
sudo pip install virtualenv
For Windows users, open Powershell as admin, and run:
pip install virtualenv
Once the install is completed, we can verify by running:
virtualenv --version
Next, let us create a new environment with Virtualenv:
virtualenv terminal-chat
Once the environment is done creating, we move into the new directory created and we activate the environment:
1# change directory 2 cd terminal-chat 3 # activate environment 4 source bin/activate
For Windows users, you can activate by running:
1# change directory 2 cd terminal-chat 3 # activate environment 4 Scripts\activate
We need to install libraries, which we will use during this project. To install them, run:
pip install termcolor pusher git+https://github.com/nlsdfnbch/Pysher.git python-dotenv
What are these packages we have installed? And what do they do? I’ll explain.
termcolor
: ANSII Color formatting for output in the terminal. This package will format the color of the output to the terminal. Note that the colors won't display in Powershell or Windows Command Prompt.pusher
: the official Python library for interacting with the Pusher HTTP API.pysher
: Python module for handling pusher WebSockets. This will handle event subscriptions using Pusherpython-dotenv
: Python module that reads the key, value pair from .env
file and adds them to the environment variable.Let us create a new .env
file which will hold our environment variables, which will be used in connecting to Pusher. Create a new file called .env
and add your pusher app id, key, secret and cluster respectively:
1PUSHER_APP_ID=YOUR_APP_ID 2 PUSHER_APP_KEY=YOUR_APP_KEY 3 PUSHER_APP_SECRET=YOUR_APP_SECRET 4 PUSHER_APP_CLUSTER=YOUR_APP_CLUSTER
Next, create a file called terminalChat.py
and add:
1import getpass 2 from termcolor import colored 3 from dotenv import load_dotenv 4 load_dotenv(dotenv_path='.env') 5 class terminalChat(): 6 pusher = None 7 channel = None 8 chatroom = None 9 clientPusher = None 10 user = None 11 users = { 12 "samuel": "samuel'spassword", 13 "daniel": "daniel'spassword", 14 "tobi": "tobi'spassword", 15 "sarah": "sarah'spassword" 16 } 17 chatrooms = ["sports", "general", "education", "health", "technology"] 18 19 ''' The entry point of the application''' 20 def main(self): 21 self.login() 22 self.selectChatroom() 23 while True: 24 self.getInput() 25 26 ''' This function handles login to the system. In a real-world app, 27 you might need to connect to API's or a database to verify users ''' 28 29 def login(self): 30 username = input("Please enter your username: ") 31 password = getpass.getpass("Please enter %s's Password:" % username) 32 if username in self.users: 33 if self.users[username] == password: 34 self.user = username 35 else: 36 print(colored("Your password is incorrect", "red")) 37 self.login() 38 else: 39 print(colored("Your username is incorrect", "red")) 40 self.login() 41 42 ''' This function is used to select which chatroom you would like to connect to ''' 43 def selectChatroom(self): 44 print(colored("Info! Available chatrooms are %s" % str(self.chatrooms), "blue")) 45 chatroom = input(colored("Please select a chatroom: ", "green")) 46 if chatroom in self.chatrooms: 47 self.chatroom = chatroom 48 self.initPusher() 49 else: 50 print(colored("No such chatroom in our list", "red")) 51 self.selectChatroom() 52 53 ''' This function is used to get the user's current message ''' 54 def getInput(self): 55 message = input(colored("{}: ".format(self.user), "green")) 56 57 if __name__ == "__main__": 58 terminalChat().main()
What is going on in the code above?
We import the colored module which will give colors to our console output and the load_env
module to load environment variables from our .env
file. We then called the load_env
function.
The terminalChat
class is then defined, with some properties:
pusher
: this property will hold the Pusher server instance once it is available.channel
: this property will hold the Pusher instance of the channel subscribed to.chatroom
: this property will hold the name of the channel the user wants to chat in.clientPusher
: this property will hold the Pusher client instance once it is available.user
: this property will hold the details of the currently logged in user.users
: this property holds a static list of users who can log in, with their values as the password. In a real-world application, this would usually be gotten from some databasechatrooms
: this property holds a list of all available chat-rooms one can join.We have four functions defined, which I will explain how they work respectively:
main
: this is the entry point into our application. Here, we call the function to log in, and the function to select a chat room. After this, we have a while loop that calls the getInput
function. This while loop means the getInput
function will always be running. This is to enable us always have an input to type in new messages to the terminal.
login
: the login function is as simple as the name implies. It is used to manage login into the app. In the function, we ask for both the username and password of the user. Next, we check if the username exists in our user’s dictionary. Also, we check if the password correlates with the user’s password. If all is well, we assign the user variable to the value of the user input.
Note: for the sake of this tutorial, we have a pre-defined dictionary of users. In your application, you may need to verify that the user exists in your database.
selectChatroom
: as the name implies, this function enables the user to select a chat-room. First, it informs the user of the available chat-rooms, before proceeding to ask us to select a chat-room. Once a valid chat-room has been selected, we assign the chat-room variable to the selected room, and we call a method called initPusher
(which we will create soon), which initializes and sets up Pusher to send and receive messages.
getInput
: this function is simple. It shows an input with the logged in user’s name in front, waiting for the user to enter a message and send. For now, it does nothing to the message, we will revisit this function once Pusher has been set up correctly.
If we remember, in the previous section above, we discussed the initPusher
method which initializes and sets up Pusher to send and receive messages. Here is where we implement that function.
First, we need to add the following imports to the top of our file:
1#terminalChat.py 2 from pusher import Pusher 3 import pysher 4 import os 5 import json
Next, let’s go ahead and defined initPusher
and some other functions within our terminalChat class:
1''' This function initializes both the Http server Pusher as well as the clientPusher''' 2 def initPusher(self): 3 self.pusher = Pusher(app_id=os.getenv('PUSHER_APP_ID', None), key=os.getenv('PUSHER_APP_KEY', None), secret=os.getenv('PUSHER_APP_SECRET', None), cluster=os.getenv('PUSHER_APP_CLUSTER', None)) 4 self.clientPusher = pysher.Pusher(os.getenv('PUSHER_APP_KEY', None), os.getenv('PUSHER_APP_CLUSTER', None)) 5 self.clientPusher.connection.bind('pusher:connection_established', self.connectHandler) 6 self.clientPusher.connect() 7 8 ''' This function is called once pusher has successfully established a connection''' 9 def connectHandler(self, data): 10 self.channel = self.clientPusher.subscribe(self.chatroom) 11 self.channel.bind('newmessage', self.pusherCallback) 12 13 ''' This function is called once pusher receives a new event ''' 14 def pusherCallback(self, message): 15 message = json.loads(message) 16 if message['user'] != self.user: 17 print(colored("{}: {}".format(message['user'], message['message']), "blue")) 18 print(colored("{}: ".format(self.user), "green"))
In the init function, we initialize a new Pusher instance to the pusher
variable, passing in our APP_ID
, APP_KEY
, APP_SECRET
and APP_CLUSTER
respectively. Next, we initialize a new Pysher
client for Pusher, passing in our APP_KEY
. We then bind to the connection, the pusher:connection_established
event, and pass the connectHandler
function as it’s callback. The reason we do this is to ensure that the client has been connected before we try to subscribe to a channel. After this is done, we call connect
on the clientPusher
.
You might have been wondering why we are using Pysher as the client library for Pusher here. It is because the default Pusher library only allows for triggering of events and not subscribing to them. Pysher is a community library which allows us to subscribe for events using Python on the server.
In the connectHandler
function, we receive an argument called data
. This comprises connection data that comes from the established connection between the Pusher WebSockets.
We subscribe to the channel, which has been chosen with Pusher, then bind to an event called newmessage
, passing in the pusherCallback
function as it’s callback.
In the pusherCallback
method, we receive an argument called message
, which returns the object of the new message received from Pusher. Here, we convert the message to a readable JSON format for Python, then check if the message isn't for the currently logged in user before printing the message to the screen alongside the sender’s name. We also print the logged in user’s name to the screen, with a colon in its front, so the user knows he can still type.
Let’s update our getInput
function, so we can trigger the message to Pusher once it is received:
1''' This function is used to get the user's current message ''' 2 def getInput(self): 3 message = input(colored("{}: ".format(self.user), "green")) 4 self.pusher.trigger(self.chatroom, u'newmessage', {"user": self.user, "message": message})
Here, after receiving the message, we trigger a newmesage
event to the current chat-room, passing the current user and the message sent.
Here is what our terminalChat.py
looks like:
1import getpass 2 from termcolor import colored 3 from pusher import Pusher 4 import pysher 5 from dotenv import load_dotenv 6 import os 7 import json 8 9 load_dotenv(dotenv_path='.env') 10 11 class terminalChat(): 12 pusher = None 13 channel = None 14 chatroom = None 15 clientPusher = None 16 user = None 17 users = { 18 "samuel": "samuel'spassword", 19 "daniel": "daniel'spassword", 20 "tobi": "tobi'spassword", 21 "sarah": "sarah'spassword" 22 } 23 chatrooms = ["sports", "general", "education", "health", "technology"] 24 25 ''' The entry point of the application''' 26 def main(self): 27 self.login() 28 self.selectChatroom() 29 while True: 30 self.getInput() 31 32 ''' This function handles logon to the system. In a real world app, 33 you might need to connect to API's or a database to verify users ''' 34 35 def login(self): 36 username = input("Please enter your username: ") 37 password = getpass.getpass("Please enter %s's Password:" % username) 38 if username in self.users: 39 if self.users[username] == password: 40 self.user = username 41 else: 42 print(colored("Your password is incorrect", "red")) 43 self.login() 44 else: 45 print(colored("Your username is incorrect", "red")) 46 self.login() 47 48 ''' This function is used to select which chatroom you would like to connect to ''' 49 def selectChatroom(self): 50 print(colored("Info! Available chatrooms are %s" % str(self.chatrooms), "blue")) 51 chatroom = input(colored("Please select a chatroom: ", "green")) 52 if chatroom in self.chatrooms: 53 self.chatroom = chatroom 54 self.initPusher() 55 else: 56 print(colored("No such chatroom in our list", "red")) 57 self.selectChatroom() 58 59 ''' This function initializes both the Http server Pusher as well as the clientPusher''' 60 def initPusher(self): 61 self.pusher = Pusher(app_id=os.getenv('PUSHER_APP_ID', None), key=os.getenv('PUSHER_APP_KEY', None), secret=os.getenv('PUSHER_APP_SECRET', None), cluster=os.getenv('PUSHER_APP_CLUSTER', None)) 62 self.clientPusher = pysher.Pusher(os.getenv('PUSHER_APP_KEY', None), os.getenv('PUSHER_APP_CLUSTER', None)) 63 self.clientPusher.connection.bind('pusher:connection_established', self.connectHandler) 64 self.clientPusher.connect() 65 66 ''' This function is called once pusher has successfully established a connection''' 67 def connectHandler(self, data): 68 self.channel = self.clientPusher.subscribe(self.chatroom) 69 self.channel.bind('newmessage', self.pusherCallback) 70 71 ''' This function is called once pusher receives a new event ''' 72 def pusherCallback(self, message): 73 message = json.loads(message) 74 if message['user'] != self.user: 75 print(colored("{}: {}".format(message['user'], message['message']), "blue")) 76 print(colored("{}: ".format(self.user), "green")) 77 78 ''' This function is used to get the user's current message ''' 79 def getInput(self): 80 message = input(colored("{}: ".format(self.user), "green")) 81 self.pusher.trigger(self.chatroom, u'newmessage', {"user": self.user, "message": message}) 82 83 84 if __name__ == "__main__": 85 terminalChat().main()
Here is what our chat looks like if we run python terminalChat.py
:
We’ve seen how straightforward it is to add realtime chats to our terminal, thanks to Pusher Channels. Our demo app is a simple example. The same functionality could be used in many real world scenarios. You can check out the source code of the completed application on GitHub, and dive deeper into Pusher Channels docs.