script to auto create a topic

NodeBB Development
  • 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');{
        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
    Running 'Chat' application on

    'Chat' index.js

    var app = require('express')();
    var http = require('http').Server(app);
    var io = require('')(http);
    app.get('/', function(req, res){
    io.on('connection', function(socket){
        console.log('a user connected');
    http.listen(3000, function(){
        console.log('listening on *:3000');

    'Chat' index.html

    <script src="/"></script>
        var socket = io('');
        socket.emit('something_custom', 'message');

    Added this line to NodeBB's '/src/socketio/index.js'

    socket.on('something_custom', function(msg) {

    I keep getting this error "XMLHttpRequest cannot load No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' is therefore not allowed access.

  • @agarcia17 said:

    No 'Access-Control-Allow-Origin'

    Stackoverflow answer :
    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
    Running Chat2 on

    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=""></script>
        var socket = io.connect('');
        socket.emit('topics.test', {data: "some data"}, function(err, result) {

    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 🙂

Suggested Topics