Cross-Domain requests with ShareJS

156 Views Asked by At

I am trying to develop a plugin for DokuWiki that will enable real-time collaborative editing of wiki pages. I am using Node.js and ShareJS to do so, but I am having some trouble since it's the first time I am using them...

What I got so far is based on this and this pages.

ShareJS Server - http://localhost:3000

var express = require('express');
var app = express();

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
    res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
    res.header('Access-Control-Allow-Credentials', true);
    return next();
});

// public folder to store assets
app.use(express.static(__dirname + '/public'));

// get sharejs dependencies
var sharejs = require('share');
require('redis');

// options for sharejs 
var options = {
    db: {type: 'redis'},
    browserChannel: { cors: "http://localhost/dokuwiki/" },
};

// attach the express server to sharejs
sharejs.server.attach(app, options);

// listen on port 3000 (for localhost) or the port defined for heroku
var port = process.env.PORT || 3000;
app.listen(port);

Which by the way outputs this warning when I run it:

enter image description here

DokuWiki

When editing a wiki page, say http://localhost/dokuwiki/doku.php?id=start&do=edit, the plugin includes these scripts:

And then executes this:

window.onload = function() {

    // get dokuwiki editor textarea element
    var pad = document.getElementById('wiki__text');

    if (pad) { // if a wiki page is being edited
        // Server options
        var options = {
            origin: "http://localhost:3000/channel"
        };

        // Connect to the server
        var connection = sharejs.open('test', 'text', options, function(error, doc) {
            doc.attach_textarea(pad);
        });
    }

};

Which lead to the following errors on the DokuWiki page editor:

enter image description here

What am I missing? Thanks in advance!

1

There are 1 best solutions below

0
On

Managed to solve it! :) The DeprecationWarning is still there and I don't know why, but it's working now.

All it took was editing the ShareJS Server code:

  • Removed the browserChannel: { cors: "http://localhost/dokuwiki/" }, line from the ShareJS options

  • Tweaked with the Access-Control-Allow-Origin middleware

Here's the "final" ShareJS Server code:

var express = require('express');
var app = express();
var port = 3000;

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

// public folder to store assets
app.use(express.static(__dirname + '/public'));

// get sharejs dependencies
var sharejs = require('share');
require('redis');

// options for sharejs 
var options = {
    db: {type: 'redis'},
};

// attach the express server to sharejs
sharejs.server.attach(app, options);

app.listen(port, function() {
    console.log('listening on *:' + port);
});

And that's it! Now I just need to edit this code a bit more to make different wiki pages have different ShareJS textareas.