Sails.js Sockets - Best Practices 4/2014 - sails.js

I believe the approach to Sockets within sails.js has changed over the last several months. A little confused on what the best practice should be.
socket.get('/comment/subscribe', function () {
socket.on('comment', function(obj) {
if (obj.verb == 'created') {
var data = obj.data;
if (data.project == 1) {
// This adds it to a visual list.
addComment("data.content");
}
}
});
});
I believe this approach is deprecated. What is the best syntax socket subscription?
Also - am I able to subscribe to a particular criteria of the model, in this case a particular comment or other attribute?

By default when you request a model, you will be subscribed to it. That is the default if you are using blueprints. There is no need to explicitly subscribe.
However, you if you are making your own controller methods. You will need to subscribe socket requests to record updates manually on the server side.
Assuming you have a User model...
To have a socket be notified of all created events call User.watch(req)
For updated and destroyed call User.subscribe(req, records)
Here is a copy of my User controller for a project, I have put comments on the lines that handle subscriptions. hope it helps....
UserController = {
find: (req, res) ->
User.watch(req) // Subscription
if req.params.all().id
query = User.findOne(id: req.params('id'))
else
query = User.find()
query.exec((err, records) =>
return res.send(500, err) if (err)
User.subscribe(req, records) // Subscription
return res.json(records)
)
create: (req, res) ->
User.create(req.body).exec((err, record) =>
return res.send(500, err) if (err)
User.publishCreate(record, record.toJSON()) // Subscription
return res.json(record.toJSON())
)
update: (req, res) ->
User.update(req.params('id'), req.body).exec((err, records) =>
return res.send(500, err) if (err)
return res.notFound() if _.isEmpty(records)
record = _.first(records)
User.publishUpdate(record.id, record.toJSON()) // Subscription
return res.json(record.toJSON())
)
destroy: (req, res) ->
User.destroy(req.params('id')).exec((err, records) =>
return res.send(500, err) if (err)
return res.notFound() if _.isEmpty(records)
record = _.first(records)
User.publishDestroy(record.id) // Subscription
return res.send(204, null)
)
me: (req, res) ->
User.findOne(
id: req.session.user_id
).done((err, user) =>
User.subscribe(req, user) // Subscription
return res.json(user)
)
}
module.exports = UserController

Assuming you want to get some data from the server via socket the code should look something like this on frontend
socket.get('/comment/subscribe', function serverResponse(data) {
// use data
})
on server side in commentController
subscribe: function(req, res) {
//some logic
res.json({
data: 'some random data'
});
}
it depends on what exactly you want to do but this is the basic structure. Also note that sails uses blueprints which is a way to make some basic CRUD routes available without coding them in the controller More info here: http://irlnathan.github.io/sailscasts/blog/2014/01/17/sailscasts-answers-ep8-how-do-blueprint-actions-and-blueprint-routes-work-in-sails/
Regarding the part of the code in your question that starts with socket.on this is an event listener on the client side listening for server to send some data. For example
socket.on('room', function messageReceived(message) {
switch (message.verb) {
case 'created':
addRoom(message.data);
break;
case 'addedTo':
postStatusMessage('room-messages-'+message.id, $('#user-'+message.addedId).text()+' has joined');
increaseRoomCount(message.id);
break;
case 'removedFrom':
postStatusMessage('room-messages-'+message.id, $('#user-'+message.removedId).text()+' has left');
decreaseRoomCount(message.id);
break;
case 'destroyed':
removeRoom(message.id);
break;
case 'messaged':
receiveRoomMessage(message.data);
default:
break;
}
});

Related

Amplify Datastore subscription cost

I am trying to understand the cost of Datastore. It seems that it subscribes to all Mutations. So if there are 50 users, then each message will be send 50 times, even if it not required.
As each real time mutation costs money, we will be paying unnecessary 49 times for this real time message mutation.
Also , it seems to me SyncExpression doesn't have any effect on this Subscription.
I am really stuck here. It will be great of someone can clarify
Amplify generates the datastore boilerplate code for you, but you still need to call it. You won't pay for every user and every mutation.
You will only subscribe to a mutation (explicitly call the code to listen for changes) on a per-user basis for things that user is interested in. e.g. if you are viewing a TODO item, you'd subscribe the user to that item and they'll immediately see if someone else modify it on another device.
UPDATE
Long story... I was triggering back-end computation via GraphQL by making a lambda resolver. The computation took too long and the GQL call would timeout. I updated the code so the GQL call called itself asynchronously (re-trigger the lambda), and returned immediately. Then when the long-running task completed in the spun-up lambda, I updated the a record in the database.
I update the record using AppSync instead of direct GQL so it would trigger mutations, and in the react client, I listen to a mutation for the specific record that will be updated. This way, there is just 1 user listening (if they've triggered the long running action) and that user is only notified about changes to the single DB record they're interested in, and not receiving other user's updates.
I don't know if all this is applicable to your situation. The code snippets below may help you, but they're somewhat out of context.
// In amplify/backend/api/projectname/schema.graphql
type Subscription {
onCouponWithIdUpdated(id: ID!): Coupon #aws_subscribe(mutations: ["updateCoupon"])
}
// In my useSendCoupon hook...
// Subscribe to coupon updates
useEffect(() => {
if (0 === couponId) {
return
}
console.log(`subscribe to coupon updates for couponId:`, couponId)
const onCouponWithIdUpdated = /* GraphQL */ `
subscription OnCouponWithIdUpdated($id: ID!) {
onCouponWithIdUpdated(id: $id) {
id
proofLink
owner
}
}
`
const subscription = API
.graphql(graphqlOperation(onCouponWithIdUpdated, { id: couponId }))
.subscribe({
next: ({ provider, value }) => {
const coupon = value.data.onCouponWithIdUpdated
//console.log(`Proof Link:`, coupon.proofLink)
setProofLinks([coupon.proofLink])
setSendCouponState(COUPON_STATE_PREVIEW_SUCCESS)
},
error: error => console.warn(error)
})
console.log('subscribed: ', subscription)
return () => {
console.log(`unsubscribe to coupon updates`)
subscription.unsubscribe()
}
}, [couponId])
// inside a lambda...
const updateCouponWithProof = async (authorization, couponId, proofLink) => {
const initializeClient = () => new AWSAppSyncClient({
url: process.env.API_XXXX_GRAPHQLAPIENDPOINTOUTPUT,
region: process.env.REGION,
auth: {
type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
jwtToken: authorization
},
disableOffline: true,
})
const executeMutation = async (mutation, operationName, variables) => {
const client = initializeClient()
try {
const response = await client.mutate({
mutation: gql(mutation),
variables,
fetchPolicy: "no-cache",
})
return response.data[operationName]
} catch (err) {
console.log("Error while trying to mutate data", err)
throw JSON.stringify(err)
}
}
const updateCoupon = /* GraphQL */ `
mutation UpdateCoupon(
$input: UpdateCouponInput!
$condition: ModelCouponConditionInput
) {
updateCoupon(input: $input, condition: $condition) {
id
proofLink
owner
}
}
`
const variables = { input: { id: couponId, proofLink } }
try {
return await executeMutation(updateCoupon, 'updateCoupon', variables)
} catch (error) {
console.log(`executeMutation error`, error)
}
}

Axios Delete Not Working In React App But Working In PostMan/Insomnia

Axios DELETE works when I send a request through postman but on my react app it doesn't. I'm passing the _id that MongoDB assigns the entry. I'm initiating ObjectId and it still doesn't work. I also double checked if I was using the correct route, which I was.
In my app I have click function that calls SaveBook. That part I feel okay about. Let me know if I need to share something else.
SaveBook in AuthActions.js on the front end
export const saveBook = ({books, user, book, _id}) => {
return function () {
console.log(`This is id ${JSON.stringify(_id)}`)
const savedIndex = books.indexOf(book);
if (savedIndex >= 0) {
console.log(savedIndex)
axios
.delete("/api/users/wishlist", {_id})
} else {
console.log(savedIndex)
// console.log(`Adding ${book.book.title} to faves...`);
axios
.post("/api/users/dashboard", {book, user})
.then(console.log("success"))
.catch (err =>
json(err)
);
}
}
};
In users.js the delete operation on the server side
router.delete('/wishlist', (req, res) => {
const db = mongoUtil.getDb();
db.db("mern-auth-2").collection("savedbooks")
.deleteOne({_id:ObjectId(req.body._id)})
.then(res.json(res.data))
});
I realized req.body wasn't the correct choice for the Delete method and used url params/ req.params to send the _id. This works well.
Fixed this line in authActions.js
axios
.delete("/api/users/wishlist/" + _id,)
Fixed these few lines in Users.js
router.delete('/wishlist/:id', (req, res) => {
const db = mongoUtil.getDb();
db.db("mern-auth-2").collection("savedbooks")
// .deleteOne({_id:ObjectId(req.body._id)})
.deleteOne({_id:ObjectId(req.params.id)})

Meteor fetch server-side job without collection

I want to submit a request from my Meteor client to the server, that has the server make an HTTP request to a website, and then return the response to the client.
On a REST web server, I would make an HTTP GET from the client to the server, which would then make its own request and respond to the client.
I haven't added a REST interface to my Meteor app and don't want to add this overhead for just this one need. However, using collections to get this done is unweildy and not the right tool.
Is there any way for the Meteor client to securely ask the server to do something and get a response without using collections? I'm messing with meteor Methods such as:
Meteor.methods({
'/http/get'(name, cbk) {
cbk = cbk || function() {};
HTTP.get('http://www.google.com', {}, (err, data) => {
cbk(err, data);
});
},
});
However this isn't seeming to work. The call is being made on the Client side.
This is exactly what Meteor methods are for.
Meteor methods docs
Server
First define your method on your server:
Meteor.methods({
// Namespace for clarity
'make.rest_call'(callback) {
HTTP.get('http://www.google.com', {}, (err, data) => {
callback(err, data);
});
}
});
OR
If you need the client to do something with the data then return a promise here (promise docs)
Meteor.methods({
// Namespace for clarity
'make.rest_call'(callback) {
return new Promise((resolve, reject) => {
HTTP.get('http://www.google.com', {}, (err, data) => {
if (err) { reject(err); }
resolve(data);
});
}
}
});
Client
Then call it from your client:
// Simple call (just makes the call, does nothing on the client)
Meteor.call('make.rest_call');
OR
// Promise based call
Meteor.call('make.rest_call', (error, result) => {
if (error) { /* do something with error */ }
// result contains your promise.
result.then((data) => {
// do something with returned data
});
});

In sails.js: fetch model's data from remote server

my sails.js app is embedded in php project where php generate some date for sails model. Can't find the way to fetch this date in beforeCreate callback or some custom method. I don't want use db to sync 2 models (model from sails & model from php). And i need to send some date to remote php app.
sails.js v.0.9.x
here is my controller:
module.exports = {
index: function (req, res) {},
create: function (req, res) {
if ( !req.param('login') )
throw new Error('Login is undefined');
if ( !req.param('message') )
throw new Error('Initial message is undefined');
var user, thread;
User.create({
id: 1,
name: req.param('login'),
ip: req.ip
}).done( function (err, model) {
user = model;
if (err) {
return res.redirect('/500', err);
}
user.fetch(); // my custom method
});
return res.view({ thread: thread });
}
};
and model:
module.exports = {
attributes: {
id: 'integer',
name: 'string',
ip: 'string',
fetch: function (url) {
var app = sails.express.app;
// suggest this but unlucky :)
app.get('/path/to/other/loc', function (req, res, next) {
console.log(req, res)
return next()
});
}
}
};
UPD My solution
Model:
beforeCreate: function (values, next) {
var options = 'http://localhost:1337/test',
get = require('http').get;
get(options, function (res) {
res.on('data', function (data) {
_.extend( values, JSON.parse( data.toString() ) );
next();
});
}).on('error', function () {
throw new Error('Unable to fetch remote data');
next();
});
}
Yikes--app.get is nowhere near what you need. That's binding a route handler inside of your app and has nothing to do with requesting remote data. Don't do this.
The easiest way to fetch remote data in Node is using the Request library. First install it in your project directory using npm install request. Then at the top of your model file:
var request = require('request');
and in your fetch method:
fetch: function(url, callback) {
request.get(url, function(error, response, body) {
return callback(error, body);
});
}
Note the added callback parameter for fetch; this is needed because the request is an asynchronous operation. That is, the call to fetch() will return immediately, but the request will take some time, and when it's done it will send the result back via the callback function. Seeing as fetch is at this point just a wrapper around request.get, I'm not sure why it's necessary to have it as a model method at all, but if the URL was based on something within the model instance then it would make sense.

Error handling with Mongoose

I am an absolute NodeJS beginner and want to create a simple REST-Webservice with Express and Mongoose.
Whats the best practice to handle errors of Mongoose in one central place?
When anywhere an database error occurs I want to return a Http-500-Error-Page with an error message:
if(error) {
res.writeHead(500, {'Content-Type': 'application/json'});
res.write('{error: "' + error + '"}');
res.end();
}
In the old tutorial http://blog-next-stage.learnboost.com/mongoose/ I read about an global error listener:
Mongoose.addListener('error',function(errObj,scope_of_error));
But this doesn't seem to work and I cannot find something in the official Mongoose documentation about this listener. Have I check for errors after every Mongo request?
If you're using Express, errors are typically handled either directly in your route or within an api built on top of mongoose, forwarding the error along to next.
app.get('/tickets', function (req, res, next) {
PlaneTickets.find({}, function (err, tickets) {
if (err) return next(err);
// or if no tickets are found maybe
if (0 === tickets.length) return next(new NotFoundError));
...
})
})
The NotFoundError could be sniffed in your error handler middleware to provide customized messaging.
Some abstraction is possible but you'll still require access to the next method in order to pass the error down the route chain.
PlaneTickets.search(term, next, function (tickets) {
// i don't like this b/c it hides whats going on and changes the (err, result) callback convention of node
})
As for centrally handling mongoose errors, theres not really one place to handle em all. Errors can be handled at several different levels:
connection errors are emitted on the connection your models are using, so
mongoose.connect(..);
mongoose.connection.on('error', handler);
// or if using separate connections
var conn = mongoose.createConnection(..);
conn.on('error', handler);
For typical queries/updates/removes the error is passed to your callback.
PlaneTickets.find({..}, function (err, tickets) {
if (err) ...
If you don't pass a callback the error is emitted on the Model if you are listening for it:
PlaneTickets.on('error', handler); // note the loss of access to the `next` method from the request!
ticket.save(); // no callback passed
If you do not pass a callback and are not listening to errors at the model level they will be emitted on the models connection.
The key take-away here is that you want access to next somehow to pass the error along.
hey this is the simplest way i found..
try { } catch (error) {
console.log(error);
// checking validation
if (error.name === "ValidationError") {
const message = Object.values(error.errors).map(value => value.message);
return res.status(400).json({
error: message
})
}
res.status(400).json(error.message)
}
}
just copy paste