Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forwarding communication #1715

Closed
peteruithoven opened this issue Aug 12, 2014 · 3 comments
Closed

Forwarding communication #1715

peteruithoven opened this issue Aug 12, 2014 · 3 comments

Comments

@peteruithoven
Copy link

I'd like to forward websocket communication (custom events, messages etc). I have to forward communication between clients because they can't reach each other directly (because they are usually behind routers etc).
Is there any way to handle any event, so I don't have to specify all the event names on my server?

I would really like to do is the following:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000);

var subject = null;

var nsp = io.of("/");
nsp.on('connection', function(socket){
  if(socket.handshake.query.isSubject) {
    subject = socket;
    // forward all events from subject to other clients in namespace
    subject.on("anything",function(data) {
      subject.broadcast.emit(data.type,data.payload);
    });
  } else {
    // forward all events from clients to subject
    socket.on("anything",function(data) {
      subject.emit(data.type,data.payload);
    }); 
  }
});

Looking through the code I found the onevent handler, and inspired by #1185 I figured out the following solution. My question is, is there a better solution?
(If there isn't I hope the solution is useful more people)

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000);

var subject = null;

var nsp = io.of("/");
nsp.on('connection', function(socket){
  if(socket.handshake.query.isSubject) {
    subject = socket;
  }

  var oneventHandler = socket.onevent;
  socket.onevent = function() {
    oneventHandler.apply(this,arguments);
    var packet = arguments[0];
    var eventType = packet.data[0];
    var eventData = packet.data[1];
    var eventCallback = packet.data[2];
    if(socket === subject) {
      // forward all events from subject to other clients in namespace
      subject.broadcast.emit(eventType,eventData);
    } else {
      // forward all events from clients to subject
      subject.emit(eventType,eventData);
    }
  };
});

This handles all events, to go even more generic you could override onpacket.

@peteruithoven
Copy link
Author

A colleague figured out a slightly more elegant way. This method creates a actual 'any' event and allows you to work with regular event listeners.

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

http.listen(3000);

var subject = null;

var nsp = io.of("/");
nsp.on('connection', function(socket){
  if(socket.handshake.query.isSubject) {
    subject = socket;
  }
  createAnyEvents(socket);

  socket.on('any', function (eventType,data,callback) {
    if(socket === subject) {
      // forward all events from subject to other clients in namespace
      subject.broadcast.emit(eventType,data);
    } else {
      // forward all events from clients to subject
      subject.emit(eventType,data,callback);
    }
  });
});
function createAnyEvents(socket) {
  var originalOnEvent = socket.onevent;
  socket.onevent = function() {
    // emit regular event
    originalOnEvent.apply(socket, arguments);
    var data = arguments[0].data;
    /** Note: turn this event into a 'any' event
    * We add the event type as first argument, the regular arguments 
    * (data and callback) become the subsequent arguments
    */
    data.unshift('any');
    // emit 'any' event
    originalOnEvent.apply(socket, arguments);
  };
}

@peteruithoven
Copy link
Author

The following package also seems interesting, but it's probably not compatible with socket.io 1.0. It tries to override onClientMessage in a Manager, but this Manager doesn't seem to exist anymore.
https://www.npmjs.org/package/socket.io-wildcard

@NathanGRomano
Copy link

There is also https://npmjs.org/package/socket.io-events which gives you more of an express style routing of events, it supports wild cards and regular expressions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants