Share-Primus is a client (browser side) extension to ShareJS that lets you use ShareJS with WebSockets, BrowserChannel, SockJS or any other streaming library supported by Primus.
Primus also has several useful plugins such as Substream, which makes it possible to use the same connection as a transport for several logical streams.
Import the scripts:
<script src="/primus/primus.js"></script>
<script src="text.js"></script>
<script src="share.uncompressed.js"></script>
<script src="share-primus.js"></script>
Create a connection and subscribe to a document:
var primus = new Primus();
var sjs = new window.sharejs.Connection(primus);
var doc = sjs.get('test-collection', 'test-doc');
doc.subscribe();
doc.whenReady(function () {
if (!doc.type) doc.create('text');
doc.attachTextarea(document.getElementById('pad'));
});
var primus = new Primus(server);
var shareClient = share.server.createClient({backend:backend});
primus.on('connection', function (spark) {
shareClient.listen(new sharePrimus.SparkStream(spark));
});
Start the server:
node example/server.js --transformer=websockets
You can also start the server with other streaming frameworks:
node example/server.js --transformer=[websockets|browserchannel|sockjs|engine.io|socket.io]
IMPORTANT! When you start the server the first time, Primus will tell you to npm install
the underlying streaming framework first. Pay close attention to the error message.
The example code is a little more sophisticated than the code above. It also sets up a substream for sending non-sharejs messages over the same connection.
Open a browser: http://localhost:7008/