2016-06-08 2 views
0

Ich habe 2 Loopback node.js Anwendung. In 1 App erstellt ich eine socket.io Server und Emittieren einige Ereignisse wie folgt ..Höre auf die Benachrichtigung von einer anderen Anwendung von node.js

app.io = require('socket.io')(app.start()); 

    app.io.sockets.on('connection', function(socket) { 
     var watcher = chokidar.watch('transactions.xlsx', {ignored: /^\./, persistent: true}); 
     watcher 
     .on('change', function(path) { 
      xlsx({ 
      input: "transactions.xlsx", 
      output: null 
      }, function(err, transactions) { 
      if(err) { 
       console.error(err); 
      } else { 
       console.log(transactions); 
       app.io.sockets.volatile.emit('notification', transactions); 
      } 
      }); 
     }); 
    }); 

Wie kann ich für diese Meldung Ereignis in app2 hören.

Mein Flow sollte sein:

Emit from app1----->listen in app2----->then send notification to client side.

Bitte teilen Sie Ihre Ideen.Vielen Dank im Voraus.

Antwort

0
  1. schaffen Verbindung für andere laufende node.js App
  2. passieren die Benachrichtigung als emittieren mit anderen Verbindungs ​​

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script src="http://localhost:3200/socket.io/socket.io.js"></script> <script> // creating a new websocket var socket = io.connect('http://localhost:3200'); var other_socket = io.connect('http://localhost:3400'); // url of other node.js app // on every message recived we print the new datas inside the #container div socket.on('notification', function (data) { // convert the json string into a valid javascript object $('#container').html(data[0].Balance); $('time').html('Last Update:' + new Date()); other_socket.emit('notification', data); }); </script>

Für Emit von app1 -----> hören in App2 direkt; Socket.io-Client-Bibliothek wie folgt verwenden -

`var io = require('socket.io-client'); 
var client1 = io.connect('socketURL1'); 
var client2 = io.connect('socketURL2'); 
client1.on('notification', function (data) { 
client2.emit('notification', JSON.stringify(data)); 
});` 
+0

thanks..But eigentlich was ich brauche, ist meine node.js App sollte als Socket-Client, der für ein Ereignis von einem anderen node.js App zu hören ist – Subburaj

+0

ok, ich bearbeite meine Antwort –

+0

Bitte verwenden Sie den unten stehenden Code in Ihrer App von einem anderen Knoten zu hören. js app (update die Socket URLs) –