script to auto create a topic
-
@esiao yeah that's actually where I'm at right now, I have this code inside a function, createTopic(), and whenever a certain hook is fired by NodeBB, createTopic() is called and the topic gets created fine. But my question is, can a third party app get NodeBB to fire this particular hook so that createTopic() gets executed?
-
@agarcia17 Basically follow & adapt what's done here
http://socket.io/get-started/chat/For the logic :
-
NodeBB is on port :4567 so you need to listen on that one.
Localhost should be fine if your instance is on the same server else you need the server adress or dns. -
Socket emit custom_event → your trigger
-
Socket on custom_event → the receiver (your function)
You can pass any data through sockets so you can easily imagine a form to fill up that you parse and the data is passed to the function.
-
-
@esiao so I attempted (socket io is brand new to me) to follow the socket io tutorial you provided me but I can't seem to get it working. If you're willing to help me out some more here's what I have.
Running NodeBB on 127.0.0.1:4567
Running 'Chat' application on 127.0.0.1:3000'Chat' index.js
var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res){ res.sendfile('index.html'); }); io.on('connection', function(socket){ console.log('a user connected'); }); http.listen(3000, function(){ console.log('listening on *:3000'); });
'Chat' index.html
<script src="/socket.io/socket.io.js"></script> <script> var socket = io('http://127.0.0.1:4567'); socket.emit('something_custom', 'message'); </script>
Added this line to NodeBB's '/src/socketio/index.js'
socket.on('something_custom', function(msg) { console.log('worked!'); });
I keep getting this error "XMLHttpRequest cannot load 127.0.0.1:4567/socketio/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000 is therefore not allowed access.
-
@agarcia17 said:
No 'Access-Control-Allow-Origin'
Stackoverflow answer : http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin
You need to enable your server to make cross-origin request for ajax. -
@esiao thanks! I got passed that error message by running chrome with these two arguments "--allow-file-access-from-files --disable-web-security" but the NodeBB server doesn't print out "worked!"
PS. I duplicated the "Chat" application and tested it this way:
Running Chat1 on 127.0.0.1:3000
Running Chat2 on 127.0.0.1:4000and I was able to Socket.emit from Chat2 and Socket.on (Chat1) was executed. so that is working.
-
finally got it working!! thanks @esiao couldn't have done it without you..!
I changed 'Chat' index.html to look like this"<script src="http://127.0.0.1:4567/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://127.0.0.1:4567'); socket.emit('topics.test', {data: "some data"}, function(err, result) { alert(result); }); </script>
I removed the line that I added to NodeBB's /src/socketio/index.js
And used @Schamper post from here where he explains how to use websockets in your plugins. So thanks @Schamper for the post it helped a lot..!