Comments (18)
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:
from nats.c.
Thank you. I read the readme.md and example only. Wonderful job and happy new year.
from nats.c.
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.
@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.
@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.
@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.
Then can you show code for your subscriber? That would be the easiest way to see what's wrong.
from nats.c.
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.
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.
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.
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.
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.
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.
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.
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.
right, sorry I just blindly c/p'd that code into my callback hah
from nats.c.
I hope I can get this resolved, but regardless, thanks for all your help!
from nats.c.
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)
- natsSubscription_Fetch not respecting timeout HOT 19
- Intermediate timeout errors when connecting to nats HOT 7
- Signature callback does not know the server URL
- Random crash in natsConn_processMsg HOT 2
- Problem with headers depending on the payload HOT 4
- natsMsg_Destroy memory used increase
- natsConnection_RequestMsg returns NATS_NO_SERVER_SUPPORT instead of NATS_TIMEOUT when there is no connection HOT 11
- Alternative TLS implementation using WinAPI Schannel HOT 1
- nats-js-pub not working HOT 1
- Ordered sync JS subscription example HOT 3
- Add NATS into Conan package manager HOT 1
- Support core affinity for subscription threads
- Observing subscriber disconnections in case of message backlog HOT 3
- NATS Core + NATS C Client + NATS CLI message loss on fast CPU's HOT 3
- Possible error in stan/pub.c HOT 1
- Tests failing `-fsanitize=thread`
- Test flappers
- callback during nats reconnecting HOT 1
- Callback for kvWatcher HOT 1
- The protobuf-c and ssh libraries are looked for when using the cnats static library. HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nats.c.