Giter VIP home page Giter VIP logo

Comments (18)

kozlovic avatar kozlovic commented on June 3, 2024

To send binary data, please use natsConnection_Publish(natsConnection *nc, const char *subj, const void *data, int dataLen).

If you have downloaded the source, please check the documentation by opening doc/html/index.html. Otherwise, you can try the following link that displays the doc directly from the repository:

https://htmlpreview.github.io/?https://raw.githubusercontent.com/nats-io/cnats/master/doc/html/group__conn_pub_group.html#gac0b9f7759ecc39b8d77807b94254f9b4

from nats.c.

nifflin avatar nifflin commented on June 3, 2024

Thank you. I read the readme.md and example only. Wonderful job and happy new year.

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

No problem. I have added documentation online (the previous was not working well). Give it a try:

http://nats-io.github.io/cnats/

Thanks and Happy New Year to you too!

from nats.c.

elimist avatar elimist commented on June 3, 2024

@kozlovic is there any way to receive binary data? No matter what I do, the message received is always NULL when subscribing to a subject that broadcasts binary data.

@metalwood did you find a way to subscribe to binary data?

sorry if this is obvious, I can't seem to find any documentation or tests as examples.

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

@elimist You simply call natsConnection_Publish with data pointing to your byte array. You need to specify the length with parameter dataLen.

Check the doc for this API here

Let me know if you still have issues.

from nats.c.

elimist avatar elimist commented on June 3, 2024

@kozlovic thanks for the reply. I'm not sure I understand what you mean by using natsConnection_Publish to subscribe to binary data.

I'm only using the cnats client to receive incoming data, and on the publishing side, I'm using the nats go client with default encoding (which supposedly sends byte arrays untouched).

When I telnet into my gnatsd server and subscribe to the binary channel, I can receive the binary data sent from my go client, so I don't think it's an issue with the sending side.

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

Then can you show code for your subscriber? That would be the easiest way to see what's wrong.

from nats.c.

elimist avatar elimist commented on June 3, 2024

The problem can be demonstrated with the simple onMsg callback from the example code. When receiving binary data, both natsMsg_GetDataLength and natsMsg_GetData return nothing.

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

Which version of cnats are you using? I just tested and works fine for me (using a go client with EncodedConn to send a byte array). If the callback is invoked, I would think that Data and DataLength should return something, unless you are sending an empty array. Can you show me how you are sending (the go client side)?

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

For example, this works:

ec, _ := nats.NewEncodedConn(nc, "default")
defer ec.Close()

msg := []byte{1, 2, 3, 4}
ec.Publish(subj, msg)

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

With the above sender, my C callback could look something like:

if (natsMsg_GetDataLength(msg) != 4)
{
    printf("Wrong size\n");
    abort();
}
else
{
    char *pbytes = (char*) natsMsg_GetData(msg);
    int i, b;

    for (i=0; i<4; i++)
    {
        b = (int)(*pbytes);
        if (b != (i+1))
        {
            printf("Expected %d, got %d\n", (i+1), b);
            abort();
        }
        pbytes++;
    }
}

and it will work.

from nats.c.

elimist avatar elimist commented on June 3, 2024

hm, that's very interesting. This is the code i've been testing with:

package main

import (
    "fmt"

    "github.com/elimist/flatbuffers-pub-test/fbs"
    flatbuffers "github.com/google/flatbuffers/go"
    "github.com/nats-io/nats"
)

func main() {
    nc, _ := nats.Connect("nats://localhost:4222")
    c, _ := nats.NewEncodedConn(nc, nats.DEFAULT_ENCODER)
    defer c.Close()

    oLat := float64(7.419505)
    oLng := float64(43.736825)

    dLat := float64(7.419758)
    dLng := float64(43.731142)

    b := flatbuffers.NewBuilder(0)

    aid := b.CreateString("test0")
    uid := b.CreateString("test1")

    fbs.LocationUpdateStart(b)
    fbs.LocationUpdateAddOrig(b, fbs.CreateCoords(b, oLat, oLng))
    fbs.LocationUpdateAddOrig(b, fbs.CreateCoords(b, dLat, dLng))
    fbs.LocationUpdateAddApptID(b, aid)
    fbs.LocationUpdateAddUserID(b, uid)
    fbs.LocationUpdateAddSpeed(b, float64(123.321))
    u := fbs.LocationUpdateEnd(b)
    b.Finish(u)
    bts := b.Bytes

    c.Publish("foo", bts)
    c.Close()
}

I'll try the above cb code and see if it works

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

The doc about EncodedConn that you pointed out specified this:

// DefaultEncoder implementation for EncodedConn.
// This encoder will leave []byte and string untouched, but will attempt to
// turn numbers into appropriate strings that can be decoded. It will also
// propely encoded and decode bools. If will encode a struct, but if you want
// to properly handle structures you should use JsonEncoder.

So only []byte and string are left untouched. That being said, could you print bts content before the call to publish?

fmt.Printf("bts=%v\n", bts)

Thanks!

from nats.c.

elimist avatar elimist commented on June 3, 2024

With the above cb code, get the wrong size message. Also, I'm using the latest cnats.
When I print the byte slice, I can see that it's non-zero.

Well, since you are obviously able to receive the binary data it seems this is most likely a problem on my end and has nothing to do with cnats. I'm calling cnats from my c++ code so it might have something to do with that, I'm not sure... time to do more investigating!

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

Well, the code I showed was assuming I was sending a byte array of length 4. But clearly, that shows that I was able to send from EncodedConn a byte array and receive it (and verify the content) in the C client.

from nats.c.

elimist avatar elimist commented on June 3, 2024

right, sorry I just blindly c/p'd that code into my callback hah

from nats.c.

elimist avatar elimist commented on June 3, 2024

I hope I can get this resolved, but regardless, thanks for all your help!

from nats.c.

kozlovic avatar kozlovic commented on June 3, 2024

I hope too. I know that the C client can receive bytes. Actually, this is the normal behavior. The String APIs are just a convenience, but really, you are sending a byte array of a given length. Receiving is the same. When receiving a message on a given subject, the message has a pointer to a byte array of a given length. I have tested that you can send byte arrays from any client and receive them from the C client.
Anything I can do to help, let me know.

from nats.c.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.