Skip to content

Transport Documentation

Adam Janin edited this page May 3, 2016 · 3 revisions

Transport Example

The following is a minimal two-way chat example. In one shell, call minchat.py chat1 chat2. In another shell, call minchat.py chat2 chat1.

import sys
from nluas.Transport import Transport

myname = sys.argv[1]
remotename = sys.argv[2]

t = Transport(myname)
t.subscribe(remotename, lambda tuple: print('Got', tuple))

while True:
    t.send(remotename, input())

This code first imports the necessary package, extracts from the command line the local chat name and the name of chat you want to communicate with, and creates a Transport object. You use the name of the Transport object to communicate with it. So if there's a Transport object t1 named chat1 and it wants to send a message to a transport object t2 named chat2, it would call t1.send('chat2', 'some message').

The subscribe call is used to receive messages. It takes as arguments the name of the remote Transport you're expecting a message from, plus a function of one argument. The function gets called whenever a message from the remote Transport is sent to the local Transport. The function's single argument is a tuple of the remote message (e.g. a python string, array, or dictionary.

The loop simply repeats forever, getting a string from the user and sending it to the remote chat. Any real example should have some way of quitting cleanly.

Transport API