script to auto create a topic



  • Hi everyone,
    So, I'm able to write a plugin that can create a topic in a certain category whenever NodeBB fires a particular hook, using this:

    var Topics = require('../../src/topics');
    
    Topics.post({
        uid: 1,
        title: 'this is a title',
        content: 'this is the content of the message', 
        cid: 27}, 
    function (err, result) {
        if (!err) {
            console.log("successfully created topic.");
        }
    });
    

    I was wondering if there is a way to have this particular block of code in a seperate javascript file and whenever that file is executed have it create the topic?



  • Yes you can. You just need an event to fire it.
    My approach would be to create a function :
    createTopic() with your script in it. Then fire this function with a custom event and then attach to something like a button a trigger of this custom event.
    You could even imagine something with inputs where you can enter the values for topic title and topic message.



  • @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?



  • anybody have an idea?



  • I'm not 100% sure but since NodeBB uses sockets why can't you send a socket from a third party tool/app to NodeBB port and then fire your function on that socket catch ?



  • @esiao thanks..! Since I'm pretty new to NodeBB would you care to elaborate a bit more on how I would accomplish this?



  • @agarcia17 Basically follow & adapt what's done here

    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 thank you sir..!!! I will look into that 🙂



  • @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:4000

    and I was able to Socket.emit from Chat2 and Socket.on (Chat1) was executed. so that is working.



  • anybody know why NodeBB doesn't receive the custom event that another application emits to it?



  • 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..! 🙂



  • @agarcia17 I'm glad I could help you. And I want to say thanks for the solution I'll probably do something along those lines for my forum too 🙂


Log in to reply
 

Suggested Topics

  • 6
  • 5
  • 1
  • 1
  • 2
| |