question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ubuntu 16.04 REP/REQ Over websockets question

See original GitHub issue

here is my server code:

const nanomsg = require('nanomsg');
const http = require('http');
const fs = require('fs');
const rep = nanomsg.socket('rep')
rep.bind('ws://127.0.0.1:7789');
rep.on('data', function(msg){
    console.log(msg)
    rep.send('received message');
})
http.createServer(function( req, res ){
  fs.createReadStream( 'index.html' ).pipe( res )
}).listen( 3000 )

and my front code:

<!DOCTYPE html>  
<html>  
<head></head>  
<body>  
   <div id="response"> 
      <span> </span> 
   </div> 
   <input type="text"> 
   <button>send</button> 
   <script> 

      var ws = new WebSocket('ws://127.0.0.1:7789',[
         'rep.sp.nanomsg.org'
      ])

      ws.onmessage = function( e ){
          var reader = new FileReader() // handle binary messages
          reader.addEventListener('loadend', function(){
             var result = reader.result;
             document.querySelector('span').textContent = result
          });
          reader.readAsText( e.data );
      }

       var button = document.querySelector('button');
       var input  = document.querySelector('input');
       button.addEventListener('click', function( e )
       {
           ws.send(input.value);
           input.value = '';
       })

   </script> 
</body>  
</html>  

from the chrome devtools I see the upload msg,but the server client doesn’t console or reply the msg .After that, i change the req/rep to pair and pub/sub, it works. Is there anything wrong in my req/rep code? Hope your help,Best wishes

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
LionCoder4evercommented, Jan 28, 2018

@reqshark thanks for your recommend advice , i solved my problem successfully! Best wishes

0reactions
reqsharkcommented, Jan 26, 2018

btw @LionCoder4ever if you’re serious about using nanomsg pub/subs for websocket in the browser, I recommend using the wsopt() socketopt

seems like you already figured out that pair and req/rep sockets are useless for doing websocket msgs over the browser in nanomsg

and avoid messing with browser’s FileReader to decode, b/c text frames are more convenient.

here’s an update to server.js that we should probably fix in examples/ws:

const addr = 'ws://' + '127.0.0.1' // or do: + require('ip').address()
const pub = require('nanomsg').socket('pub')
const sub = require('nanomsg').socket('sub')

/* set the nanomsg websocket text frames option */
pub.wsopt('text')
sub.wsopt('text')

/* use case doesnt need buffers, set stream encoding to utf8 */
sub.setEncoding('utf8')

/* websocket connections with some random ports */
pub.bind(addr + ':7789')
sub.bind(addr + ':7790')

/* recv websocket msgs */
sub.on('data', recv)

function recv(msg) {
  console.log(msg)
  pub.send(`${msg} ... from node-nanomsg`)
}

/* delete/replace this with your browser server code */
require('http')
  .createServer(function( req, res ){
    require('fs')
      .createReadStream( 'index.html' )
      .pipe( res )
  })
  .listen( 3000 )

console.log('please check http://localhost:3000')

now with text frames sockopts set, your index.html javascript is a bit more reasonable:

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div class='res'><span></span></div>
  <input type='text' />
  <button>send</button>
  <script>
    var addr = 'ws://127.0.0.1'
    var sub = new WebSocket(addr + ':7789', [ 'pub.sp.nanomsg.org' ])
    var pub = new WebSocket(addr + ':7790', [ 'sub.sp.nanomsg.org' ])

    /* recv websocket msgs */
    sub.onmessage = recv

    function recv(msg) {
      document
        .querySelector('span')
        .textContent = msg.data /* <-- an improvement from FileReader :) */
    }

    /* send websocket msgs w/ input button */
    var input = document.querySelector('input')
    document
      .querySelector('button')
      .addEventListener('click', send)

    function send( e ){
      pub.send(input.value)
      input.value = ''
    }

    /* call send() also if user hits return key */
    input.onkeydown = function hitreturn ( e ) {
      if (e.keyCode === 13 || e.which === 13)
        send()
    }
  </script>
</body>
</html>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Websockets on Ubuntu 16.04 - java
I am trying to create a discord bot, and it requires being able to connect to a websocket, but however, whenever I attempt...
Read more >
Fix list for IBM WebSphere Application Server V8.5
IBM WebSphere Application Server provides periodic fixes for the base and Network Deployment editions of release V8.5. The following is a complete listing ......
Read more >
Ratchet PHP Websocket persistent Ubuntu 16.04
Try to use SERVERNAME instead localhost. From PHP RATCHET: Run your website and WebSocket server on the same machine using port 8080 for ......
Read more >
16 WebSockets Interview Questions Web Devs Must Know
The key word in that definition is two-way: with WebSocket, both the client and the server can trigger communication with one another, and...
Read more >
Websocket connetion fails on droplet with Ubuntu 14.04.
I created and tested a Java EE 7 application that uses WebSockets on my local PC. All works fine when I deploy to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found