Build

Fun With PubNub in the Terminal Using Curses and Python

3 min read Michael Carroll on Feb 27, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

PubNub in the Terminal! Check out our terminal client for fast testing and debugging from any computer.

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!

More from PubNub

NPP and HIPAA: Notice of Privacy Practices Definition
Healthcare CategoryJan 6, 20235 min read

NPP and HIPAA: Notice of Privacy Practices Definition

A Notice of Privacy Practices (NPP) is one of the requirements of HIPAA and helps patients understand their personal data rights.

Michael Carroll

Michael Carroll

HIPAA Violation Examples
Healthcare CategoryJan 5, 20236 min read

HIPAA Violation Examples

HIPAA violations can be financially expensive and devastating to a brand. Examine some examples of HIPAA violations, and learn...

Michael Carroll

Michael Carroll

HIPAA Technical Safeguards: How To Protect Sensitive Data
Healthcare CategoryJan 5, 20236 min read

HIPAA Technical Safeguards: How To Protect Sensitive Data

HIPAA covered entities must follow the five technical safeguards to achieve HIPAA compliance and prevent data corruption.

Michael Carroll

Michael Carroll