I want to use Primus instead of socket-io with simplewebrtc. Can any one help me to convert the below code to use Primus? https://github.com/andyet/SimpleWebRTC/blob/master/socketioconnection.js
Asked
Active
Viewed 665 times
1 Answers
1
How this works is that you can pass any constructor as the connection
property to the config, and any additional options you need. Then, all your constructor needs to do is export an object with the four methods: on
, emit
, getSessionId
, and disconnect
. For Primus, you could have something like this (I've never used Primus before, just looking at docs) -
var Primus = window.Primus; // get primus from wherever you want
function PrimusConnection(config) {
var self = this;
var primus = new Primus(config.url, config.primus);
primus.on('connection', function (spark) {
// spark is the new connection.
self.connection = spark;
});
}
PrimusConnection.prototype.on = function (eventName, function) {
this.connection.on(eventName, function);
};
PrimusConnection.prototype.emit = function () {
this.connection.write.apply(this.connection, arguments);
};
PrimusConnection.prototype.getSessionid = function () {
return this.connection.id;
};
PrimusConnection.prototype.disconnect = function () {
return this.connection.end.apply(this.connection, arguments);
};
module.exports = PrimusConnection;

xdumaine
- 10,096
- 6
- 62
- 103
-
Thank you very much @xdumaine. As per discussion with Primus team, I have to use Primus at server side as well. Currently we developed server side part using Spring-websockets which internally uses SockJS. So primus team told it will not work with SockJS. So I am going to modify above code to use sockjs-client. – pnreddy Dec 15 '15 at 05:19