updated example to terminate after getting messages