I'm new to node.js, so i have some questions about connect framework and socket.io:
What's different? i'm confused about it.
Should i use connect fw with socket.io or just use socket.io?
The Connect module is a web application framework, while Socket.IO is a realtime transport module. You would use one to create web applications, and the other for bidirectional communication between a server and a client.
Here's a few of the things the modules can do:
Connect:
service static files and pages
provide cookie-based sessions
accept file uploads
handle HTTP verbs (GET/POST/PUT/DELETE)
Socket.IO:
authorize connecting sockets
send data between server and client with multiple transports
supports (WebSocket/XHR long-polling/flashsocket/JSONP)
So if you wanted to create a website, you would use Connect. However, if you wanted that website to have something such as realtime chat capability, then you would use Socket.IO.
Whether you should use one module or the other, or use them together, is dependent on your application requirements.
Connect is special module which can provide scalable functionality. You can just add features as middleware. It reminds some kind of configuration of your project, it just simplify routine.
var app = connect()
.use(connect.logger('dev'))
.use(connect.static('public'))
.use(connect.bodyParser())
.listen(3000);
After adding this for example you can access features which connect provide. For example you can have logging (method url and seconds) for each application activity, or add session support, easy with one line of code. The same way you can add socket support I suppose.
Related
I have the current APIs running in an express server along with MongoDB in form of HTTP requests response. I currently have the use case of a messaging system that I know requires web sockets.
Should I rewrite the whole APIs in socket.io? Or Is there any option to do it on top of the existing HTTP server
Any suggestions in this situation will be helpful.
No, the are not mutually exclusive. Keep using HTTP for things that make sense as APIs, and only add sockets for the things that require bi-directional messaging.
socket.io is not required, and it's usually better to just use plain websockets. socket.io is a old, large framework that's not needed.
I am in the process of developing a chat application using Javascript. When sending messages from one client to another client, do I have to send it through a server or can I send it directly from a peer-to-peer approach, using something like websockets ?
Welcome to the stage of life where you see the importance of design patterns.
You can start solutionizing with mediator pattern and proxy pattern with web sockets.
Wheater you need a server or not is up to your design.
Technology-wise there are multiple APIs that HTML5 offers you can go through them and make something on your own.
There is a bunch of APIs available with HTML5 and JS.
Start digging on WebSockets, Server-Sent Events, Web Workers.
The server will give you the flexibility of record-keeping while acting as a mediator. Alternatively, you can come up with a pure p2p design with a scheme where every node or user notify other users with their details(IP) for establishing communication. Remember for web socket to work the client need to know what address to connect to. Maybe it can have fixed master nodes. Then you can use observables for polling and other features. Take a look at the BitTorrent protocol for design inspiration.
Get creative and start designing.
There are many ways to do it. I recommend the scheme:
Peer <---> custom websocket server <---> Peer;
I recommend NodeJS with SocketIO.
I'm building a full-stack web application. Backend is done in express, front-end in Vue.
The application has some real-time features, and for this I am using socket.io. Socket IO will have to keep track of some variables and react with different events to clients depending on current state of those variables. I intend to use redis to manage application state.
I would like to keep my API stateless, and separate my socket server from my API server (they would run on different ports). I would like to do this because I feel like it will be easier to manage, more scalable, and I think my security will benefit from this.
For authorized actions, client will send data to API, API will process the data and send it to the socket, and socket will emit changes to clients.
For unauthorized actions, I will use direct socket to client communication.
Is this a good way to structure my application?
I am building an app in which I provide functionality X, Y and chat.
Lets say that X and Y are non-interactive eg. reading articles - which will work fine with REST (on a node.js server) while chat is obviously interactive so it will work best with socket.io!
Questions: 1. Is it possible for me to 'switch on' a socket between the server and the user when the user navigates to the chat part of the application? 2. Can I open up a socket inside a GET request for the url: example.com/chats/usr_id on the node.js server?
3. How can this be accomplished inside a Backbone routing framework?
Yes. Just initialize the connection when the view is rendered (via a controller or script). See socket.io client documentation. You can just connect when the view is rendered and disconnect when the view is terminated. http://socket.io/docs/client-api/
You cannot open sockets with a GET request. Socket.io has it's own build in mechanisms for connecting to a socket server. It will start with Web Socket protocol and fall back to Long Polling. You can however use custom url's for unique things. One again, consult the socket.io documentation: http://socket.io/docs/client-api/
http://www.sitepoint.com/chat-application-using-socket-io/
p.s. I'd suggest reading up on how Web Sockets work, as you don't seem to have a very strong understanding.
Is there anyway to send data though sockets from Node.JS to SignalR? I have a Node.JS app that sends realtime information as JSON format. The other app it's an MVC C# app that uses SignalR to send the data to the client via socket. I want tosen data from de nodejs to signalr and signal send that info to the client.
You might consider better solution for internal communication between processes. SignalR is meant to be used between .Net server and client using different authentication, handshake, protocol and network layer methods, which is inefficient for internal server communication.
Take a look on ZeroMQ, is well simple and very easy to use tool, meant especially for such cases. It has bindings for most languages including .Net and node.js.
There is js client for browser to communicate with Signal R server.
http://www.nuget.org/packages/SignalR.Js
You probably can extract js file from it and run from Node.js.
And probably standard Socket.IO will just work, you need to subscribe to proper events and go.
If you want a node.js client for signalR that doesn't require jQuery I started this one. It intentionally only supports websockets.
https://npmjs.org/package/signalr-client