As a gentle introduction to using fszmq, we'll create a simple client\server example. Specifically, we'll create two sockets. One socket, acting as the server, will bind to a TCP endpoint. Whenever it gets a request message it will take some action. Meanwhile, the other socket will act as a client. It will connect to the server's TCP endpoint and issue a number of requests. For each reply, it will take some action.
To start, we'll reference our library and open the
fszmq namespace, which defines the
Socket type and the
(Note: don't worry too much about contexts just yet. For now, assume every node in your network has one
which owns one, or more,
Socket instances.) We'll also open some modules which contain functions for working with contexts and sockets.
1: 2: 3: 4:
Now here's our basic server that takes "hello" requests and replies with "world". Any other message causes it to exit.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
And here's a simple client that makes 10 requests and prints the server's reply each time.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
If you run this example in F# Interactive, you should see the following:
Notice how our two sockets are communicating synchronously. In other words, the client sends one request and must wait for a reply. Conversely, the server waits for a single request and immediately responds to it. If this were not the case, the parenthetical numbers at the start of each output line could be out of order.
But don't think this is the only way to use fszmq! Go look at the other samples, or read the zguide, to see examples of asynchronous client\server, publish\subscribe, map\reduce, and many other distributed computing patterns. And, most of all, have fun using F# ("Simple code for complex problems") and ZeroMQ ("Distributed computing made simple")!
Full name: docs.PATH.hijack
new : unit -> fszmq.Context
Full name: Tutorial.server
Full name: fszmq.Context.rep
Full name: fszmq.Socket.bind
Full name: fszmq.Socket.recv
Full name: docs.decode
Full name: Tutorial.client
Full name: fszmq.Context.req
Full name: fszmq.Socket.connect
Full name: docs.encode
Full name: fszmq.Socket.send
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: docs.spawn
Full name: docs.PATH.release