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.
sails.sockets.addRoomMembersToRooms(ServiceHeartBeatSession._classRoom()
, ServiceHeartBeatSession._room(session.id) );
Then, the invoke method is executed to send this event to all listening WebSockets.
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:
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.
sails.sockets.broadcast([ ServiceHeartBeatSession._room(session.id) ], "serviceheartbeatsession", 'hi!');
- The following works as intended, but I prefer not to use that.
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.
var emitter = sails.io.sockets;
emitter.in(‘sails_model_serviceheartbeatsession_5b6b26748377829d34983d67:serviceheartbeatsession’); // for example
emitter.emit(‘serviceheartbeatsession’, { verb: “created” });
Check more information in the article: https://adevait.com/nodejs/building-nodejs-apis-with-sailsjs
Anyone have any ideas?