Fix bi-directional messaging.

Better tests using Sinon.
Better error handling for JSON parse errors.