Build

Fun With PubNub in the Terminal Using Curses and Python

3 min read Michael Carroll on Feb 27, 2014

One of the great features of PubNub is that it can be used virtually anywhere. PubNub has clients for everything, from Java to JavaScript to Haskell. This is great for developers building applications and servers in multiple languages, but what about debugging and testing? We here at PubNub test our service every day, so we decided it was a good idea to create a terminal client for fast testing from any computer.

PubNub terminal testing

This image shows a screenshot of our PubNub Curses Developer Console. You can also checkout our standard web-based PubNub Developer Console.

Getting Started

To build our client, I used Python and Curses. Curses is a window manager for the terminal that allows you show multiple things going on at a time. This is much like the top command. Also, since it is in Python, I took advantage of the package manager for deployment. Installation is simple:

Using the PubNub Python SDK

Python terminal testingOnce in the client, the controls should be visible on the screen. To start off you can hit “p” to start publishing. You will then be put into edit mode where you can type some valid JSON syntax and hit “Ctrl-G” to publish that message. You should then see your message appear in the subscribe window. To scroll through the subscribe window, use the up and down arrows. Using the “r” key you can re-publish your last message and see it come up in the subscribe window a few times.

The client also supports Presence and Storage & Playback. Anyone who subscribes to the same channel using the same subscribe and publish keys will show up in the presence window. The history window will give you a list of the last 100 messages in a channel. You can press “h” to refresh the history view as well as use “j” and “k” to scroll this window.

The last feature is my favorite which is auto-publishing. Hitting “a” puts you into edit mode for the publish window. You can then type valid JSON and hit “Ctrl-G” to start auto-publishing. This will publish a message every 5 seconds until you hit any key on the keyboard. This feature is great for continuously having input to test while developing. Finally you can use “q” to quit the application.

Using Python Curses with PubNub

PubNub in the terminal testingThe Python curses client is well built but limited in its feature set. Coming from a JavaScript and CSS background, the limitations are well known. All the windows are fixed sizes and hard to change once the client is open.

The first thing the application does is draw text at the top of the screen. To do this we use the curses client window and add strings to it in a given x and y position. To add the first line, for instance, we calculate the offset for each string we want to draw and put them all one after the other. Not doing this calculation could mean drawing on top of other characters on the screen.

Another interesting issue I ran into was drawing a list of messages on the screen. Curses has no idea of scrolling up or down so you have to provide a way of storing and drawing only the messages you see yourself. I accomplished this by setting a curses and keeping every message drawn in the window. I then find the subset of messages we can display in the window and loop through each one to draw it:

Check out the source code for more insights on how I tackled each issue in curses.

Debugging Made Easy

Here at PubNub, the team was excited by the ability to test PubNub without ever leaving the terminal. Much of our work is done directly in the terminal and opening a web page to debug can be tedious. The ability to open up a terminal application and have all the PubNub features not only alleviates that but also makes it easier to start working with our API. Anyone can now be publishing PubNub messages in a few minutes!

0