Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we get some FsNetMQ exmaples? Or do they already exist somewhere? #1

Open
travis-leith opened this issue May 8, 2018 · 6 comments

Comments

@travis-leith
Copy link

No description provided.

@somdoron
Copy link
Contributor

somdoron commented May 8, 2018

I will try to write some today

@somdoron
Copy link
Contributor

somdoron commented May 8, 2018

Just added some examples.

Will try to add some more during the week. Let me know if you need more help

@travis-leith
Copy link
Author

got it thanks!

@travis-leith
Copy link
Author

travis-leith commented May 9, 2018

by the way, there are several typos in the example code.
for instance

use router = Socket.router ()
router.bind socket "tcp://*:6566"

should be

use router = Socket.router ()
Socket.bind router "tcp://*:6566"

and
use subscriber = Socker.sub ()

should be

use subscriber = Socket.sub ()

Also, I tried to put together a working version of the NetMQ router-dealer example so that you could include it as an official example, but I am having difficulty porting it. Here is a snippet of the attempt

[1 .. 3]
        |> List.iter(fun i ->
            use client = Socket.dealer()
            match client with
            |Socket.Socket s -> 
                s.Options.Identity = Encoding.Unicode.GetBytes(sprintf "Client %i" i)
                |> ignore

            let dealerObservable =
                Poller.addSocket poller client
                |> Observable.map (fun socket -> socket |> Socket.Socket |> Frame.recv)
                |> Observable.subscribe (fun msg -> 
                    ()//not sure how to read the frames with this
                )

            async{
                let rec infiniteLoop () =
                    sprintf "This is client %i" i
                    |> (fun s -> Encoding.Unicode.GetBytes(s))
                    |> Frame.send client

                    Thread.Sleep(delay)
                    infiniteLoop()

                infiniteLoop()
            } |> Async.Start
        )

Is any of this the recommended approach?

Edit: this is how I would do it using regular NetMQ from F#, which seems to work.

let delay = 3000

    use poller = new NetMQPoller()


    [1..3]
    |> List.iter(fun i ->
        async{
            use client = new DealerSocket()
            let clientName = sprintf "Client %i" i
            client.Options.Identity = Encoding.Unicode.GetBytes(clientName) |> ignore
            client.Connect("tcp://127.0.0.1:5556");
            client.ReceiveReady.Add (fun eventArgs ->
                printfn "Received a message from the server"
                printfn "%s" (eventArgs.Socket.ReceiveFrameString())
            )
            poller.Add(client)

            let rec infiniteLoop() =
                let messageToServer = new NetMQMessage()
                messageToServer.AppendEmptyFrame()
                messageToServer.Append(sprintf "Hello this is %s" clientName)
                printfn("======================================")
                printfn(" OUTGOING MESSAGE TO SERVER ")
                printfn("======================================")
                client.SendMultipartMessage(messageToServer)
                Thread.Sleep(delay)
                printfn "Sleep over"

                infiniteLoop()

            infiniteLoop()
        }
        |> Async.Start
    )
  
    poller.RunAsync()

@poroburu
Copy link

poroburu commented May 1, 2023

I also have this problem with the polling example. Here is the Ionide error tooltip highlighting Frame.recv:

Type mismatch. Expecting a
    'NetMQ.NetMQSocket -> 'a'    
but given a
    'Socket.T -> byte array * bool'    
The type 'NetMQ.NetMQSocket' does not match the type 'Socket.T'F# Compiler[1](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-messages/fs0001)
val recv:
      T
   -> byte array * bool

Full name: FsNetMQ.Frame.recv

Assembly: FsNetMQ

@poroburu
Copy link

poroburu commented May 1, 2023

For those who are going through the zguide with fsx scripts.
#r "nuget:FsNetMQ,0.5.0-pre"

Works better now, oops 😹 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants