void-dragon / nanomsg-browser Goto Github PK
View Code? Open in Web Editor NEWnanomsg websocket connection for the browser
License: MIT License
nanomsg websocket connection for the browser
License: MIT License
Hi, I encountered a bug when trying to send a message using the PAIR socket. The websocket closed for no apparent reason. Later i discovered that when it closes the event carries an error code, which gave me 1003. Which according to https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent means unsupported data. Please log it in the debug mode. It's too useful to be ignored.
The solution was to convert the string manually to something the websocket supports like an ArrayBuffer. I got this working with the nng pair example code available here: https://nanomsg.org/gettingstarted/nng/pair.html
I debugged your code on Repl.it, so I can give you the link to a working example: https://repl.it/repls/StrongExternalFan
Anyway, thank you very much for publishing your code. It saved me loads of time regardless of this bug.
As a bonus, your code seems to be compatible with NNG. Hurray 🥳
Hello,
First of all, thank you for this great project. I am currently using it to communicate with server-side nng endpoints and this works great.
I would like to know if the following additions might interest you:
What do you think?
On all browsers it works just fine, except when using Firefox (67.0.4 , Ubuntu18.04 ), it never connects.
in debugmode I see this:
nanomsg connect to: ws://0.0.0.0:5555/
and somewhat later
Firefox can’t establish a connection to the server at ws://0.0.0.0:5555/.
nanomsg close: ws://0.0.0.0:5555/
It seems it never gets to ws.onopen
Other browsers work and correctly show:
nanomsg connect to: ws://0.0.0.0:5555/
nanomsg.js:1 nanomsg connected: ws://0.0.0.0:5555/
Setting the package wide configuration variables have no effect when used with modules. For example, the following:
import nanomsg from 'nanomsg-browser';
nanomsg.receiveArrayBuffer = true;
nanomsg.debug = true;
const sub = new nanomsg.Socket(nanomsg.SUB);
sub.connect(subaddr);
sub.on('data', (msg) => {
console.log(typeof msg);
});
logs "string"
, while I would expect an Array Buffer.
I believe the problem is here:
if (typeof exports !== 'undefined') {
Object.assign(exports, nanomsg);
}
Which means exports
is now a copy of the nanomsg
object, not a reference, and changes to it are not reflected in the object the Socket
methods see. Keeping the CommonJS syntax, you can instead do:
if (typeof module !== 'undefined') {
module.exports = nanomsg;
}
I'm not sure how this will work with other systems such as ES6 modules, but I suspect that will properly will probably require something different. Adding getters and setters might be a solution.
You already use npm
for package install like this:
npm install nanomsg-browser
But need to support import like this:
<script type="text/javascript">
import nanomsg from 'nanomsg-browser'
// Milliseconds between reconnects.
nanomsg.reconnectTime = 10 * 1000;
// Show some debug logging in the console.
nanomsg.debug = true;
// Receives ArrayBuffer objects instead of strings. Default is `false`.
nanomsg.receiveArrayBuffer = false;
</script>
Instead of brutal import from node_modules
Just use export default nanomsg
for example
Hi,
Have you ever tested it with nng version? As stated in the documentation, nng is wire compatible with nanomsg. I am just curious about using Connect method instead of Dial to connect to a nng socket.
Afshin
Current implementation cannot deal binary data because it uses 'readAsText()' to read the data.
(There are same problem in 'send()' because it uses 'charCodeAt()')
What do you think?
Is it reasonable to add option to use binary data, like below?
nanomsg.binary = true
Hello, I may be wrong, but in this example in the readme:
// be hippster, be async/await
await sock.connect('ws://myhost:8080');
const answer = await sock.send('some cool msg');
console.log('got =>', msg);
shouldn't you log answer
to the console instead of msg
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.