Skip to content
monnand edited this page Nov 12, 2013 · 13 revisions

NOTE: uniqush-conn is in the very early stage and may contain serious bugs. Please use it at your own risk.

Install uniqush-conn

Right now, we do not provide any binary files. You need to first install go environment and use go get command to install the program(s).

go get github.com/uniqush/uniqush-conn

It should install uniqush-conn in your $GOPATH/bin or $GOBIN directory.

Then Install redis, which can be found in most GNU/Linux distro's repo.

Install the examples

To play with our example, you need to install some other programs, which supposed to be client and app servers.

go get github.com/uniqush/uniqush-conn/examples/printer
go get github.com/uniqush/uniqush-conn/examples/testclient

Now you should be able to run printer and testclient programs.

Check your tools

Now let's check if you have correctly installed the required tools:

$ echo $GOPATH
/home/monnand/goproj
$ go get github.com/uniqush/uniqush-conn
$ go get github.com/uniqush/uniqush-conn/examples/printer
$ go get github.com/uniqush/uniqush-conn/examples/testclient
$ which uniqush-conn
/home/monnand/goproj/bin/uniqush-conn
$ which printer
/home/monnand/goproj/bin/printer
$ which testclient
/home/monnand/goproj/bin/testclient
$ 

You output may be different (very likely), but should be similar.

Create a playground

Well. I don't want to mess up your file system, so let's create a directory to store all things we need:

$ mkdir -p ~/playground
$ cd ~/playground

Generate RSA keys

We need to generate the server's private and public key. A client needs the public key to make sure it is connecting with the correct server. The server should keep its private key somewhere safe.

$ openssl genrsa -out key.pem 2048
Generating RSA private key, 2048 bit long modulus
...........................................+++
...............+++
e is 65537 (0x10001)
$ ls
key.pem
$ openssl rsa -in key.pem -pubout > pub.pem
writing RSA key 
$ ls
key.pem  pub.pem
$   

Create config.yaml

Copy and paste the Example config.yaml to the file config.yaml under the playground/ directory.

Let's play!

Now here comes the big party. Open 4 terminal windows and get ready to play. I will refer each terminal window as terminal 1, 2, 3, 4 respectively.

Terminal 1

First things first, we need to start a instance of uniqush-conn

$ uniqush-conn -config=config.yaml -key=key.pem -port=8964

No need to explain the arguments. They explain themselves.

Terminal 2

Let's run printer on this terminal.

$ printer

What does printer do? It is a very very simple HTTP server: Whenever it receives an HTTP request, it will print out the path of the request to the standard output, then reply the client with an HTTP response with status code 200. We will see it later.

Terminal 3

Let start a client here with username usr1:

$ testclient -u usr1 -key pub.pem 127.0.0.1:8964

The client program will be there waiting for your input. Let's hold on a second and send something later.

Terminal 4

Similarly, we start a client here with user name usr2:

$ testclient -u usr2 -key pub.pem 127.0.0.1:8964

Sending messages to the server

Back to terminal 3, let's send something to server:

$ testclient -u usr1 -key pub.pem 127.0.0.1:8964
hello server

Well. Nothing happened here. Let's move to terminal 2, see what happened there:

$ printer
/auth:
{"service":"service","username":"usr1","token":"","addr":"127.0.0.1:57790"}
/auth:
{"service":"service","username":"usr2","token":"","addr":"127.0.0.1:57861"}
/msg:
{"connId":"13233b9da53f13ee-4d65822107fcfd52","msg":{"body":"aGVsbG8gc2VydmVyCg=="},"service":"service","username":"usr1"}

You can easily tell the structure of the output by the indentation. More precisely, there are 5 HTTP requests, each with different paths. First two requests are requests to the path /auth. You may be able to tell what they are now, but let's move on to the last request, whose path is /msg. The content of the last request, {"connId":"13233b9da53f13ee-4d65822107fcfd52","msg":{"body":"aGVsbG8gc2VydmVyCg=="},"service":"service","username":"usr1"} explains what happened: A message from usr1 under service service whose body is some magical words.

Now you can briefly understand how it works: Client sends a message to uniqush-conn and uniqush-conn will send an HTTP request to printer containing the details of the message. How can we specify which URL to go when we received a message? Read config.yaml and you will find the answer.

Sending messages between clients

Move to terminal 3, type in the following message:

usr2: hello urs2!

Move to terminal 4, see what happened:

{"msg":{"body":"IGhlbGxvIHVyczIhCg=="},"id":"13233f6c6ac025d0-55104dc76695721d","sender":"usr1","sender-service":"service"}

usr1:  hello urs2!

Well. I think there's no need to explain.

Sending messages from server to clients

Now, we need another terminal window, let's call it terminal 5.

On terminal 5, we execute the following command:

$ curl http://127.0.0.1:8088/send.json -d '{"receiver-service":"service","receivers":["usr1", "usr2"],"ttl": 3600000000000, "msg":{"header":{"title":"hello"}}}' 
{"results":[{"conn-id":"13233b9da53f13ee-4d65822107fcfd52","visible":true,"Username":"usr1","service":"service"},{"conn-id":"13233f3d0fa51260-78629a0f5f3f164f","visible":true,"Username":"usr2","service":"service"}]}

Then move back to terminal 3 and 4, we will see the following message on terminal 3:

{"msg":{"header":{"title":"hello"}},"id":"132340131d259070-55104dc76695721d"}

Similar thing happened on terminal 4:

{"msg":{"header":{"title":"hello"}},"id":"132340131d318b1e-380704bb7b4d7c03"}

Things we didn't cover

There are many functions we did not cover here, like:

  • Push notification mechanism
  • Storing the message when user is offline; let user retrieve the cached message once they back online
  • Message digest