Hi all
We would use your module nodejs, because they are very powerfull for our applications.
Right now we have a Drupal Webseite and a Mobile Website wich ist not running with drupal, only a jQuery Mobile Website.
Now we want to use socket.io on our Mobile Website to connect to the Drupal Website. They works fine, so if we send from Drupal broadcast notifications we receve it also on our Mobile Website.
But now we want to send a message back from Mobile Website to Drupal (function sendMessageToServer), the node.js Server tell us the following
Received message from client SIhsrQBkHIqgRHASAAAF
Received unauthorised message from client: cannot write to client SIhsrQBkHIqgRHASAAAF
Did anybody can help us?
<html>
<head>
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
</head>
<body>
<script>
// Create SocketIO instance, connect
var socket = io.connect('http://localhost:8080');
// Add a connect listener
socket.on('connect', function() {
console.log('Client has connected to the server!');
});
// Add a connect listener
socket.on('message', function(obj) {
console.log('Received a message from the server!', obj);
alert(obj.data.subject + ' - ' + obj.data.body);
});
// Add a disconnect listener
socket.on('disconnect', function() {
console.log('The client has disconnected!');
});
// Sends a message to the server via sockets
function sendMessageToServer() {
//socket.send(message);
var message = {
type: 'nodeToDrupal',
messageBody: 'Hello from the client side!'
};
socket.emit('message', message);
alert('send');
};
socket.on('nodejs_notify', function (data) {
alert(data);
});
</script>
<a href="javascript:sendMessageToServer()">send text</a>
</body>
</html>
Comments
Comment #1
candelas CreditAttribution: candelas commented@cola did you got a solution? I am having the same problem.
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedThe code that shows this error message is in client-manager.js (from the newest node.js server for this module now distributedon Github) is:
if (this.channelIsClientWritable(message.channel) && this.clientIsInChannel(socketId, message.channel)) {
So either this channel is not writable or your client's socket ID is not listed in the channel. Unfortunately the documentation for this module is pretty scanty so you're probably going to have to read through the code & experiment a lot to find out what you need to do for anything beyond the basics that ship with the module.
For this problem you'd have to set the property
isClientWritable
on the channel, & get your client's ID into the channel list *before* trying to write to it. Also make sureclientsCanWriteToChannels: true
is in node.js.config.js.Comment #3
glekli CreditAttribution: glekli commentedI think you guys just pointed out a missing feature. As @rhclayto says,
isClientWritablen
needs to be TRUE in order for you to be able to send messages from the client to a channel. However, there seems to be no way to set that to TRUE. Let me take a closer look.That said, when you want to send something from the browser to Drupal, there is not too much benefit in going through the Node.js server, because connection between Node.js and Drupal is not persistent. You might as well send an ajax request to Drupal directly.
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedto set some background for this setting - you *must* trust all of your clients if you turn this on.
it is easy for this setting to lead to XSS on your site if an authenticated socket connection is accessed by a bad actor.
this option was intended to be used by apps that a) trust all the clients connected and b) require client to client communication without passing through Drupal.
as Gergely points out above, if you want to send messages to the Drupal site, you are best served by an ajax request.