Skip to content

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

Open

Description

@travis-leith
No description provided.

Activity

  1. somdoron commented on May 8, 2018

    @somdoron
    Contributor

    I will try to write some today

  2. somdoron commented on May 8, 2018

    @somdoron
    Contributor

    Just added some examples.

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

  3. travis-leith commented on May 8, 2018

    @travis-leith
    Author

    got it thanks!

  4. travis-leith commented on May 9, 2018

    @travis-leith
    Author

    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()
    
  5. poroburu commented on May 1, 2023

    @poroburu

    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
    
  6. poroburu commented on May 1, 2023

    @poroburu

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions