Sails 0.9.4, socket.io, newbie - node.js

Just getting started with Sails & Socket.io. I'm following the docs and setup a simple test project here:
https://github.com/timfulmer/sails-sockets
According to the Sails docs, http://sailsjs.org/#!documentation/sockets, socket subscriptions are setup on the first socket call:
socket.get(), socket.post(), etc. are methods available in the client-side SDK included in new Sails projects. In this example, we'll use them to talk to the backend via Socket.io. Please be aware that you can use these methods whether or not you're using CRUD blueprints.
The test project defines a quick model/controller, with CRUD methods. It connects to the socket using socket.get, and receives previously posted model instances correctly.
Posting a new instance using socket.post makes it to the Sails server and creates the new instance. However, the new instance is never sent to the connection created with socket.get. Even when running the page in two different browser tabs.
Also, posting using a GET request from the browser hangs and never returns.
Am I reading the docs incorrectly or making some other newb mistake?
Thanks,
-- Tim

Ok, turned out to be a total newbie error. Sails does not magically call the function passed into socket.get when new messages are emitted. Sails is magic, but not that magic. Turns out one must implement what to do with new messages in assets/js/app.js. Problem between chair and keyboard, sails rocks!
EDIT
Updated the sample project to work with Sails.js + Socket.io + Backbone.js + CORS, with Backbone.js frontend hosted separately (in s3) than the Sails.js backend:
https://github.com/timfulmer/sails-sockets
Lots of little gotchas involved.

Related

Reactjs and Mongodb connection

I have a fundamental question about Reactjs and Mongodb. I want to build a react app which has a "search" feature that shows results from the database. However, I have an issue on understanding how to implement the connection between the react app and the database.
Sorry for the general question. Every help or hint on how to proceed will be appreciated. Thanks in advance.
React is completely back-end agnostic. Which means you would have to implement the connection yourself.
The regular way is, you setup MongoDB and a Node.js server (or whatever back-end you like) then you connect the Node.js server to MongoDB (via MongoDBs JavaScript SDK) and your React client, which runs in the browser to your Node.js server (via HTTP, express framework could help here).
Browser -> Node.js -> MongoDB.
But MongoDB also has a REST interface you could use directly via the browser, like it's mentioned in the following answer:
https://stackoverflow.com/a/16277603/1016383
Would probably be okay for small proof of concepts or experiements,.

Angular 6 webapp that connects to PostgreSQL using sequelize-typescript

I'm using Angular6, PostgreSQL and sequelize-typescript. The goal is to connect to a DB and get a list of names and ages into a table.
I started by creating an Express server.
I then used sequelize-typescript to connect to my PostgreSQL DB and call this connection when initializing server
I created a model of my data element using sequelize-typescript and using it to connect to my PosgreSQL DB.
I then created a simple route to the data using Express Router
I added the route to the initial Express Server
This all seemed to work (albeit messy as I'm learning as I go). But I can reach my local server through the browser (localhost:3000) and use PostMan to get the data from the DB (localhost:3000/people).
Client side:
I already have the initial front-end app that was created with I ng'ed the project using command line. I can reach this at (localhost:4200)
I created a simple component called people using ng g component people.
How do I get the data from my server route into the client component? I believe it's through HTTP and then subscribe but there seems to be a lot of different ways to do this. I'm looking for advice on the best pattern to use in Angular6.
TL;DR How do I get the data from my route to the front end
I finally got it working correctly, I needed to create a service that listened to the API url and from there I could return the observable to the component.
The solution (even though it's not cleaned up properly) sits at:
https://github.com/micklynch/travelbudget

Correct way to update frontend when backend changes

I'm currently setting up the following application:
Node backend with Express
Postgres DB with Knex as an interface
React frontend
Everything is working as intended and I am making good progress, my question is more architectural:
What is the preferred/recommended/best way to notify the frontend when database changes occur?
I saw that Postgres has a LISTEN/NOTIFY feature but that is not currently (ever) supported by Knex (https://github.com/tgriesser/knex/issues/285).
My thoughts:
Polling (every x seconds query the DB). This seems wasteful and antiquated but it would be easy to set up.
Sockets. Rewrite all my Express endpoints to use sockets?
?
I'm interested to see how others handle this.
Thanks!
I've had a similar situation before. I have a front end which connects via web sockets to the API. The API emits a message on successful database commit with the API endpoint matching the update. The front end components listen for these update socket messages and if the updated type is relevant to that component the component will query the API endpoint over https for the new data. Using a web socket only to advertise that an update is available won't necessitate rewriting the entire API.

adding web socket chat into existing nodejs app

I've made nodejs application by this template. And now I want to add simple websocket chat.
My question is: do I have to completely rewrite that application to add websocket chat or I can to save that structure?
You can create a chat using Socket.IO (or another library), it's perfectly possible (and probably a best practice even) to separate the two: the regular server and the WebSocket server.
The two aren't tied together.
I have never used the express MVC template, but socket.io does not use express routes and from my experience they exist side by side just fine. Just add your socket.io server code to app.js to test it out and you can use the client side code within any of your express views.
This assumes you're using socket.io of course. I have no experience with other methods of using websockets with node.js.

Looking for lazy loading server/client ActiveRecord ORM for Node.js

I'm new to node.js and I'm looking for an Active-Record-style ORM (to whichever database - any would be great). But in particular, I'm hoping to find one with these qualities:
Same interface on both client and server.
If information is already on the client, then that information is used unless it is deemed "stale".
If information is not already on the client, then it's retrieved from the server - though there should be bundling of requests so that you're not issuing a ton of http requests.
Simple (enough).
Is it out there?
there is a great framework for Node called meteor:
meteor
it seems to do what you wish and even more.

Resources