nats-io / nats.ws

WebSocket NATS
Apache License 2.0
329 stars 29 forks source link

Subscription object is non-iteratable in react-native typescript #152

Closed fiianalytics closed 2 years ago

fiianalytics commented 2 years ago

React native: 0.68 nats-ws: 1.8.1

We are using nats from the below path: import { ConnectionOptions, NatsConnection, StringCodec, Subscription } from '[nats.ws/lib/src/mod](http://nats.ws/lib/src/mod)';

Nats is getting successfully connected, and we are subscribing to a topic. On trying to loop the subscription response we are getting the following issue:

LogBox.js:173 Possible Unhandled Promise Rejection (id: 6): TypeError: Invalid attempt to iterate non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method. TypeError: Invalid attempt to iterate non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.

We must fallback to using callbacks again. Is there any other solution for this?

aricart commented 2 years ago

@fiianalytics can you give me a better context on where the subscription is created? if this is jetstream, did you await the subscription?

The subscriptions are all iterable unless you provide a callback.

fiianalytics commented 2 years ago

@aricart Thank you.

We are not using jetstream. Just trying the sample code from documentation in react native. We had some issues with textEncoder and URLsearchParams, and we had to add polyfills to fix those. And importing from nats.js did not help us. So, we had to import connect from 'nats.ws/lib/src/mod'.

Yeah, callbacks looks to work fine. not sure why subscriptions are not iterable :(

import { Connect, StringCodec } from '[nats.ws/lib/src/mod]';

const nc = await connect({ servers: "demo.nats.io:4222" });
const sc = StringCodec();
const sub = nc.subscribe("hello");

(async () => {
  for await (const m of sub) {
    console.log(`[${sub.getProcessed()}]: ${sc.decode(m.data)}`);
  }
  console.log("subscription closed");
})();

nc.publish("hello", sc.encode("world"));
nc.publish("hello", sc.encode("again"));

await nc.drain();
aricart commented 2 years ago

The import is wrong. Make sure you have the latest, of nats.ws, and react native. Importing just form 'nats.ws', should be all you need. If you don't, that means that the react stack is not current and the bundler is old. If that is the case you can try importing nats.ws/cjs/nats.js. If you are using esm modules, import nats.ws/esm/nats.js. no other import should be required.

aricart commented 2 years ago

The import is wrong. Make sure you have the latest, of nats.ws, and react native. Importing just form 'nats.ws', should be all you need. If you don't, that means that the react stack is not current and the bundler is old. If that is the case you can try importing nats.ws/cus/nats.js. If you are using edm modules, import nats.ws/esm/nats.js. no other import should be required.

From the look of it you are using import so try esm.

aricart commented 2 years ago

@fiianalytics any luck?

aricart commented 2 years ago

closing as no activity on this issue.