Using websockets (socket.io) for sails 0.10.x - node.js

I am developing a node app on sails, my app needs to do regular exchange of data between server and client
The SailsCast video demonstrates the process for sails 0.9.x, and the mentioned files are not present in Sails 0.10.x
Also the sails doc says we should be using sails.socket instead of sails.io as the later will be deprecated in coming versions
Can any one help me in exchanging data between server and client through sockets. I have done it through node but with Sails MVC, i am not sure how to put the pieces together.
Any reference link or suggestion will be of great help
Thanks

I had the same trouble. You may want to check SailsSocket doc.
If you are loading sails.io.js in your page and haven't changed the default
io.sails.autoConnect to false, then a WebSocket should be created for you.
You can access it via io.socket
Create e.g. assets/js/dependencies/app.io.js with:
io.socket.on('connect', function socketConnected() {
console.debug("This is from the connect: ", io.socket);
console.debug("WebSocket is connected:", io.socket.isConnected());
});
Make sure to load this file after sails.io.js (See pipeline.js).
Open you browser console and double check.
Also check SailsSocket methods
Hope this help you get started

Related

How can you force #google-cloud/datastore to use 'http' when you query your datastore emulator

I have a node + vuejs app from which I'm trying to query my local db (created using datastore emulator).
My code is something like this
const { Datastore } = require('#google-cloud/datastore')
const datastore = new Datastore({
apiEndpoint: "http://localhost:<port>",
projectId: <my_project_name>
})
I then try to run one of the sample queries on Google's documentation
My output shows there is a successful connection but then I get the following error in the console
POST https://localhost:<port>/$rpc/google.datastore.v1.Datastore/RunQuery net::ERR_SSL_PROTOCOL_ERROR
From the error, I see that it has changed http://localhost:<port> to https://localhost:<port> i.e it is forcing a protocol of https for my local host which then obviously fails.
Is there something/option that I have to specify to ensure that it uses http when I'm making local calls or using the datastore emulator?
I found the solution and am posting it in case someone else has the issue.
I started going through the code itself and discovered a reference to this link. The page says
.....By default, the client library will use gRPC, which is a binary tranport based on HTTP/2. It's Node.js implementation, #grpc/grpc-js, uses Node.js http2 module.
If you need to use the client library in non-Node.js environment or when gRPC cannot be used for any reason, you can use the HTTP/1 fallback mode. In this mode, a special browser-compatible transport implementation is used instead of gRPC transport.
In browser context (if the window object is defined) the fallback mode is enabled automatically; set options.fallback to false if you need to override this behavior....
When I added fallback:false to the options, everything worked i.e. it no longer redirected to https and it connected to my data emulator.
I thought about it further and I believe this behavior is because my code was written in the renderer process for a Vuejs App which means there's a windows object. To confirm, I moved the code to the main process and did not include the fallback option and it worked.

Make a logger for Node Js

I have a project in Node Js, which executes the project on port 3000 and I access from ngrok with my browser to said localhost port, and it executes a server on port 3001 to make requests to a Maria database db. The project is done in react and the server with express.
I want to save the application logs (errors, warnings, etc.) in a log file so that I can see them whenever I want.
My intention was to use winston, and while I have no problem on the server side (3001), when I try to adapt it to the main project, I get an error that it cannot save files (the reason that appears is that it runs from the browser, and you can't create such a file because you don't have access to the project folders)
Can anyone give me some advice? Am I wrong to use winston, and should I use another?
Greetings and thanks
I've never used winston before and I couldn't find anything online about your error. In the past I've always just used node's fs module to create a log of errors and restarts.
const fs = require('fs')
Node's File System Documentation: https://nodejs.dev/learn/the-nodejs-fs-module
Short YouTube Tutorial: https://www.youtube.com/watch?v=U57kU311-nE

expressJS exporting other things in addition to app

In my web application (I'm using expressJS), there are many services (such as mongoDB connection, MQTT connection, etc.) that need to be executed once the whole application is executed (using npm start command). Therefore, I can make use of these services in my entire application. For example, I want to use my MQTT connection in different files.
My idea is to export the MQTT connection, MongoDB connection, etc. in addition to the app this way:
//app.js
module.exports = {
app: app,
mqttConnection: myMQTTConnection,
db: myMongoDB
};
However, we know that this approach doesn't work (I tested it and got an error saying: TypeError: app.set is not a function).
How can I export other things in addition to app from app.js file?
If my approach is not possible, what other approaches can I use? (considering the fact that many services (such as connecting to a server, etc.) are asynchronous)

Exposing Meteor to Global namespace

Using Meteor I used to be able to open up the console and directly call Meteor methods.
console.log(Meteor.userId())
Now I get a reference error that Meteor is not defined. How do I go about exposing the Meteor object?
Just open console and refresh the page (with console open), then Meteor will be available
You need to do var Meteor = require('meteor/meteor').Meteor; in the console to access Meteor.

socket.io authentication with socketio-auth

I've a server in node.js & Express and i use socket.io for real-time messaging and socketio-auth module for authentication,I setup everything for using this module but i found nothing about db object and findUser that the auther used in examples(not even an single line of comment about them) to work with,should i implement them myself,right?
if anyone have a working example of using or implementing them with redis i'll be grateful to see it.
the db and findUser object is from MongoDb and not related to socket.io. I would recommend you look at http://www.tutorialspoint.com/mongodb/ to gain a better understanding as to why they are used

Resources