简体   繁体   中英

How to send message from one socket.io node application to other socket.io node application

In my node application I need to send message to other node application using socket.io. Because depending upon the first application results I need to do some tasks in second application. How can I do this?

         var express=require('express');
         var http=require('http');
         var app=express();
          app.configure(function(){
           app.use(express.static(__dirname + '/public'));
          });

        var server = http.createServer(app);


       var socket = require('socket.io-client')('http://localhost:3000');
       socket.on('connect', function(){
            socket.on('connected', function(data){

             console.log('connected');
           });
            socket.on('disconnect', fucntion(){

            console.log('disconnected');
             });
       });


      var io = require('socket.io-client').listen(server);
      server.listen(6509);

You can use socket.io-client in order to use socket.io as a client in your nodejs server side.

Here is an example how to work with this client:

On you'r server

var socket = require('socket.io-client')('http://localhost');
socket.on('connect', function(){
    socket.on('event', function(data){});
    socket.on('disconnect', function(){});
});

For more information on how to achieve this task see: https://github.com/LearnBoost/socket.io-client

Try

var socket = require('socket.io-client').connect('http://localhost:3000');
socket.on('connect', function(){
       socket.on('connected', function(data){

             console.log('connected');
       });
       socket.on('disconnect', function(){

            console.log('disconnected');
       });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM