How to notify client's browser about some event on server? - javascript

I'm using python (Tornado) on server side and some javascript on a client side. I have the common situation - one user send a message to another. And I want server to notify client's browser (reciever of the message) about new message. How can I do it? Should I establish long-alive connection with client (maybe using websocket) or something else?
PS
For establishing conenction via websocket I found good library TornadIO
PS2
So, due to high load of project establishing connection betwebb server and each client looks suspicious. I afraid of c10k problem. May be it's only lack of my knowledge.

I'm using python (Tornado) on server side and some javascript on a client side. I have the common situation - one user send a message to another. And I want server to notify client's browser (reciever of the message) about new message. How can I do it? Should I establish long-alive connection with client (maybe using websocket) or something else?
Using a realtime web server such as Tornado - yes. TornadIO looks like a good solution since it'll use socket.io which has fallback options for older browsers.
The Wikipedia entry on the C10k issues provides a list of servers that have resolved this problem:
A few web servers have been developed to counter the C10K problem:
nginx, which relies on an event-driven (asynchronous) architecture, instead of threads, to handle requests (WordPress.com uses nginx to solve the C10K problem)[2]
Lighttpd, which relies on an asynchronous architecture to handle requests[3]
Cherokee, a lightweight web server[4]
Tornado, a non-blocking web server and web application framework[5] written in Python
Apache Deft, asynchronous, non-blocking web server running on the JVM
JBoss Netty, a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients[6]
Node.js, asynchronous, non-blocking web server running on Google's V8 JavaScript engine[7]
EventMachine, an asynchronous, non-blocking web server running on Ruby EventMachine
Yaws, a web server written in Erlang; profiting from Erlang's extremely lightweight processes.
Medusa, a non-blocking web server library written in Python
As you'll see, Tornado is listed.
Beyond this your question is potentially more about horizontal scaling than it is about how to achieve server to client notifications.
Depending on which realtime server solution you choose this might never become an issues. For example, a single instance of Caplin System's Liberator can achieve a lot more than 10,000 persistent connections.

Related

From javascript browser client-side to Postgresql [duplicate]

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

Client-side Javascript server - possible?

Great day, community.
Question is next: is that possible to run simple HTTP server on client-side javascript which will be able to receive requests from global network and somehow process them?
For example, in a node.js I can run server with following code:
var http = require('http');
http.createServer().listen(3000, '127.0.0.1');
and then I'll have server running on 127.0.0.1:3000, I'm wonder to know is something similar to this can be implemented with a regular client-side javascript?
The definitions of "client" and "server" are relative. Node can be a server when it's sending data to clients; a Node app can also be a client to another server (for example, when you make an API call).
It sounds like you're asking if you can create a "server" using JS in the browser. You can't, but that's because most browsers are designed to only be clients, not servers — they can only make requests, not respond to them. In particular, Node itself connects to system-level sockets, which enable it to be a server. Browsers don't allow your Javascript code access to those system-level sockets, which is why it isn't possible.
Hypothetically, if they did, then you'd end up back at Node. Or recreating your own version of Node.
Note that you do have WebSockets in the browser; any other "client" can be on the "other side" of that socket. So you could implement a rudimentary client/server setup that way, but it wouldn't work with other HTTP clients.

Is there a browser-based Websocket listener implementation?

I am interested to know if anyone has built a javascript websocket listener for a browser. Basically the server side of a websocket that runs in a client. This would allow messages to be sent to the client directly. Why? Because instead of having a Node.js, python, java, etc, server process sitting on or near the client/browser, I can just use a thread in the browser as a listening server thread. I don't think that any browsers support this currently.
I've run across answers like this: https://news.ycombinator.com/item?id=2316132
Just curious if anyone has done this. I believe that the current Websockets spec does not support listeners on the browser. It would make the deployment of various peer-to-peer applications a bit easier to deploy.
WebRTC allows for peer-to-peer connections to be made between browsers.
You would still need a server in order for individual users to discover each other but then they could connect directly to each other rather than having to pass all their traffic via a central server.
The idea.
You can use a simple echo server written in any language. Your script can send the data to the server then get it back, handle it on the same page with different functions/classes emulating the real server.
An example: http://www.websocket.org/echo.html
Then, you can think about different formats of packets to/from server to diffirentiate them inside one script.

html javascript connect to raw socket

I have a c# tcp server, I want to connect to the server via a html client page.
The problem: There is no simple way to create TCP sockets in Javascript on a browser side. Although solutions like Websockets allow to create something that resemble sockets, you can use them to connect only to servers that support Websockets. Not to any random servers that know nothing about HTTP.
so is there a solution to connect to my srver.
No. There just isn't. The browser is a tightly locked down environment. The only socket connection that you can open from JavaScript is WebSocket. Since it's your server, adding WebSocket support shouldn't be too complicated, and there are WebSocket libraries available for C#.
Maybe someone else will have an idea for you, but...
The best solution I can think of is for your server to support websockets.
The situation you described - along with connectivity issues for traffic passing through proxies and routers - is one of the reasons Websockets were introduced in the first place.
Bare in mind that Websockets can send and receive binary data. It's just that javascript make it more comfortable to write text based messages.
Also, many NAT routers, Proxies and firewalls will block raw TCP/IP communication while allowing Http communication to pass through. This is why you have a better chance at connection establishment and retention when implementing the Websocket protocol.

What role does Socket.IO have with Node.js

I'm fairly new to the world of JS and its abundance of libraries. I'm looking to get into a project that involves network communication (sockets) between clients and a server. In a world with tons of libraries, I cannot make a decision as to which to use. I'm looking for something that will bring efficiency and stability.
I've been told that Node.js is like the middleman between you, as the developer, and Socket.IO. I've been told it's a huge framework that you may not use at least half of. I've been told that to maximize efficiency, you're better off using Socket.IO to make your own functionalities. I've done some research on my own and found that Socket.IO NEEDS Node.js and Node.js DOESN'T NEED Socket.IO. Which is completely opposite of what I was told. Then I find that most developers use both Socket.IO and Node.js at the same time?
Like I said, I'm fairly new, but I cannot find the right resources that would help me accomplish a websocket communication between a client and a server with maximum efficiency, or at least explain the difference between Socket.IO and Node.js. If anyone here could, please let me know! I would greatly appreciate it.
node.js is a general purpose javascript-based run-time environment (somewhat similar to other language runtimes like python in scope). You can create apps in it that don't even use the network. It is often used as a web server for created web apps and has a great set of tools and rich library of add-ons for doing so. It does not need socket.io.
socket.io is a specific library to enable web-socket-like communication between a client and a server (e.g. a chat room app is the canonical example). The server side of socket.io assumes a javascript run-time (because it's written in javascript) so that generally means node.js (though I'm not sure if a different JS runtime could perhaps be substituted).
You can think of node.js like the platform and socket.io like a specific tool to do a specific job that runs on that platform. You would use socket.io (on top of node.js) if you needed web socket connectivity between client and server.
You would use only node.js if you need any of the other things node is good at, but did not need websocket connectivity.
websockets themselves can be programmed on the server side without socket.io and without node.js. They could be programmed in strait C++ or in Java. But socket.io (running in node) provides a very easy way to set them up because the socket.io library covers both client and server in one library and one API and it's all in the same language (javascript). Look at the chat room app example on the socket.io site and you will be unlikely to find any other solution that can accomplish that in as few lines of code as it does and with the same interface on client and server.
If you were only setting up a websocket server (no web server or web app of any kind), you could still use node and socket.io and use it just for the websocket server and it would still be quite efficient. While node is capable of doing lots of other things, if you don't configure and install all those other things, they aren't costing you anything - they are just unused capabilities that aren't running.
I should add that one other thing the socket.io library does is it handles an auto-negotiation between client and server to find the best channel for the client and server to communicate on. If websockets are available, then socket.io will likely use them, but if web sockets are not available, socket.io has alternate methods that will work (even in older browsers). That functionality comes for free in socket.io without you even doing anything.
In case this isn't completely clear to you, websockets are typically used to provide real-time communication between client and server. While clients can ask for data from a server at any time with an ajax call or a web page request, what websockets allow is a two way real-time communication between client and server and the biggest advantage of websockets is that a server can send a client real-time data at any time while they are connected.
For example, I have a web page that receives real-time data from my server anytime the web page is open. The web page is served over the typical node.js web server installation, but the real-time data is sent from server to client over a websocket connection.
In addition, if there's a chatty conversation happening between client and server, websockets can be much more efficient than a series of ajax calls because with a websocket, a connection is opened once and used repeatedly whereas with ajax, each successive ajax call is like a new connection.
Node.js is a runtime environment. It's a javascript engine with a standard library built around asynchronous I/O. It plays the same role that Java, Python, Ruby, .NET, etc., play for many other web applications.
I've been told it's a huge framework that you may not use at least half of.
It might be true that most people never use most of the standard library, but I wouldn't think it's more true of Node.js than other runtimes. "Framework" isn't an accurate word to describe it.
I've been told that to maximize efficiency, you're better off using Socket.IO to make your own functionalities.
Whoever told you that was mistaken, or meant that to maximize efficiency, you're better off using [Node.js and] Socket.IO [instead of other solutions]. Many other non-Node.js solutions require a single thread or process per connection, which limits the number of simultaneous connections a server can handle. Node.js is built around asynchronous I/O which is better for keeping many connections open at once, and Socket.IO is a library for Node.js for using WebSockets.
TL;DR: Socket.IO can fire events in realtime between your client and server, so there is no need for you to reload the page to notice something changing. This can be used for "live" applications like collaborative drawing, live chats, online games and more!

Categories

Resources