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.
Related
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,.
I'm struggling with socket.io to make a chat app with the users that are logged in in my app. I followed the beginner course and I could make a private chat app, but the users aren't the ones that are in my database or they have to create a username to connect to the chat app.
In my search I found this one -
Realtime app with Vue, Laravel, Socket.io and Redis (He's making a similar app using Laravel)
I want the exact same app but using nodejs mongoose express.
Can you help me with some references or tutorials?
Here are some resources that might -
Make A Real-Time Chat Room using Node Webkit, Socket.io, and MEAN
Simple Chat Application using NodeJS and Socket.IO
Please start with any of these and be more specific with what extra feature you're trying to implement. (code would be very helpful)
Couldn't understand what do you mean by the users not in database bit.
I built an app and i'm planning to make a real time battle with Angular 2 and laravel. For example, you hit the "attack" button, and your opponent see his life going down in real time.
My app built with:
frontend: Angular 2
Backend: PHP Laravel 5.2
Now I'm searching and learning for my real time battle component,
and I saw different guides and tutorials for it:
https://www.codetutorial.io/laravel-5-and-socket-io-tutorial/
http://4dev.tech/2016/02/creating-a-live-auction-app-with-angular-2-node-js-and-socket-io/
The first tutorial is about how to use Laravel 5 and socket io.
The second one is how to use Angular 2 with NODS JS and socket io.
When I say real time, I mean that both users see the same thing that is happening on the screen)
My Backend and Frontend are totally divided and I have no setup with NodeJS anywhere in my app.
Both users need to see actions happening during a battle in my app, and It need to go through my laravel API and shown via my Angular 2 battle component
My question is -
What's the best approach to real time app (seem websockets) using Angular2 and Laravel 5.2 to get the desired result of what I'm trying to achieve?
Laravel in this context is just templating and serving the client files, and acting as an interface inbetween the client and the socket.io server. It doesn't actually act as the socket.io server, and I don't believe it can.
So yes, you would still need something (node) to host the socket.io server to interact with the client, through PHP or otherwise. Personally, I'd skip Laravel/PHP altogether and just use node with koa/express/whatever to template your client (html/js/css/etc) files. Feels like an unnecessary abstraction to me.
The code below from socket.blade.php already has a connection to the actual socket.io server, so I don't see why the additional overhead of an HTTP POST through PHP/Laravel is a good idea. Security, perhaps, but you can handle that with the actual socket.io server as well.
var socket = io.connect('http://localhost:8890');
socket.on('message', function (data) {
$( "#messages" ).append( "<p>"+data+"</p>" );
});
For the real-time character of your use-case, websockets are definitely the way to go. The players that should get the updates should be in the same 'room', so you can broadcast changes more easily. For the other functionality you can either use websockets or regular API calls to your backend directly from your client-side app code with some kind of communication between your api and the socket server, e.g. through Redis.
TLDR:
All data through sockets, node server does api calls and broadcasts changes to active players
Use API from app directly, use pub/sub queue foo for communication between laravel and node to broadcast changes to active players
Option 1:
Angular frontend app
Set up websocket connection
Add triggers for game foo which will send data over the socket connection and is handled by your nodeserver
Only talks to sockets
Node server
Serves frontend app
Handles socket connections, divide players per game
Handles socket calls and calls laravel api to do mutations on your data
Process action and broadcast changes to players in game X
Laravel REST API
Auth x
Default CRUD foo
Option 2:
Angular frontend app
Talks to api directly
Uses sockets to listen for updates
Node server
Serves frontend app
Handle websocket data
Listen on queue for published data from API
Broadcast changes to players in game x over socket
Laravel REST API
Auth
Crud
Mutation x triggers publish in Redis or other queue, which the node server can/should listen on
I'm sure there are more ways you can set this up, you just have to decide where you want what. Maybe introducing Redis is something you do not want, in that case your node app will have more to do. If you do want to use something like Redis, you'll need to do API calls from either your frontend app or choose to do it through the node app anyway, combining the 2 options.
If you are planning to use websockets then there seems to be less use of laravel as only one socket is pretty capable of handling all the data that will be exchanged between the frontend and the backend, so if you don't mind changing your engine you can try Meteor, https://www.meteor.com/
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.
I am learning HTML5 and doing so by building a simple chatroom using Express, PassportJS, Mongoose/MongoDB, connect-mongoose, NowJS.
Everything works perfectly, except for one big problem: I am having trouble authenticating NowJS.
The usual way of doing this is to read the "this.user.cookie" property server-side and parse the string. However, for some reason, cookies is not being sent back to the server. (details here: NowJS cookie field in this.user is empty) After a lot of googling, I think there are no alternative, secured, way for me to authenticate NowJS connections/clients.
Question
I am thinking of stripping all of NowJS out of my web app, and using socket.io directly. Is socket.io easy with work with? Would I lose key functionality if I switch to socket.io, instead of using NowJS?
Can I use socket.io to:
1) Call server-side functions?
2) Share server-side variables with the client?
Socket.io does not share variables or allow you to call server side functions. It allows you to bind and emit events on the client side and server side.
As for your cookie not being sent, its most likely that its being considered a cors, cross domain request, this can happen if your using a different port for socket.io then the http server that set the cookie.