Results 1 to 1 of 1

Thread: Sails.JS's publish method doesn't work in a controller, but works in the console

  1. #1
    Join Date
    Jun 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sails.JS's publish method doesn't work in a controller, but works in the console

    Currently, I'm exploring web development to see what all the hype is about. Except for following a small 'create your first CRUD app in Node.JS' and some formal training in JavaScript, I have no experience.

    I'm new to JavaScript, NodeJS, Sails.JS, and WebSockets.

    My current problem is this:

    The app I'm trying to develop receives POST calls with a JSON object. This is the input part of the application. These POST calls originate from any random host.

    The output is a webpage that updates itself by listening on a Sails.JS powered WebSocket system called sails.io.js.

    Basically, each POST from any host should result in an event on the WebSocket.

    Since I'm not building a formal REST API, I have to define some parts myself in Sails.JS. I do have blueprints enabled.

    First, in the controller that handles the POST call, I can call the code below so existing WebSockets can receive an event of a new ServiceHeartBeatSession being created. All the server side code mentioned here is located in the controller or executed in the console.

    Code:
    sails.sockets.addRoomMembersToRooms(ServiceHeartBeatSession._classRoom()
     , ServiceHeartBeatSession._room(session.id) );
    Then, the invoke method is executed to send this event to all listening WebSockets.

    Code:
    ServiceHeartBeatSession.publish([ session.id ], { "verb": "created" });
    However, the .publish method does not result in a event to an existing WebSocket. Unless I execute it myself in the console. The WebSocket is registered like this:

    Code:
    io.socket.on('serviceheartbeatsession', function(resData) {
        alert('Something changed!!!!');
      });
    I did some digging into the sourcecode and made some attempts at debugging this myself. Here's what I've found so far:

    Running the .publish call in the console produces the desired result.
    It appears that the .addRoomMembersToRooms is a requirement for the .publish to work in the console. So I'm assuming that .addRoomMembersToRooms works as intended.
    I tried the alternative below, but that doesn't work in the controller. However, it does work in the Node console.

    Code:
    sails.sockets.broadcast([ ServiceHeartBeatSession._room(session.id) ], "serviceheartbeatsession", 'hi!');
    The following works as intended, but I prefer not to use that.

    Code:
    sails.io.sockets.emit('serviceheartbeatsession', { verb: 'created' });
    The above attempt lacked a room selection, so I dug a little deeper. The calls below where the result of a .publish method. However, only after repeating this in the console, it worked.

    Code:
    var emitter = sails.io.sockets;
    emitter.in('sails_model_serviceheartbeatsession_5b6b26748377829d34983d67:serviceheartbeatsession'); // for example
    emitter.emit('serviceheartbeatsession', { verb: "created" });
    Check out the article: https://adevait.com/nodejs/building-nodejs-apis-with-sailsjs
    Anyone have any ideas?
    Last edited by james438; 07-01-2021 at 04:02 AM. Reason: removed hotlink

Similar Threads

  1. Object doesn't support this property or Method
    By visions in forum JavaScript
    Replies: 12
    Last Post: 12-30-2014, 03:02 PM
  2. Replies: 7
    Last Post: 01-01-2011, 06:15 PM
  3. Replies: 7
    Last Post: 02-13-2009, 07:51 AM
  4. Object doesn't support this property or method
    By joseph_russell in forum JavaScript
    Replies: 4
    Last Post: 05-03-2007, 07:56 PM
  5. doesn't work in IE; works fine in Mozilla
    By karine in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 02-05-2005, 03:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •