handshaking a SQL server with Javascript - javascript

I want to try, as a learning excersise, get my javascript to chat to sql.
var ws = new WebSocket("ws://127.0.0.1:1433");
doesn't seem to be a blocked port, so it should in theory work.
I am looking for a breakdown of how to handshake with the sql server, and chat with it.
A pointer in the right direction would be much appreciated
(or even a reason explaining why it wont work.)
I want to try this on Microsoft SQL 2008 R2.

MS SQL doesn't have a text based protocol to allow you to interface with it through telnet. You can use a web socket to determine of the target server is listening on 1433, but you're best bet for completing the login sequence is to use a sql client api.

SQL Server connections use the TDS protocol, which is documented in the Tabular Data Stream Protocol Specification. If you follow the protocol specification, consult the protocol examples and have a peek at the FreeTDS open source implementation you should be able to do a low level handshake and more, using basic sockets. However, there really really isn't any point in doing so besides an academic exercise. But the nail in the coffin is the WebSockets, which are not basic sockets.
The way to go is to expose the SQL Server database to the web using a web service interface preferable REST, possibly OData) and then consume this web service from your Javascript HTML5 application. Here is a good read: Creating an OData API for StackOverflow including XML and JSON in 30 minutes.

In HTML5, JavaScript can communicate directly to SQL with SQLite. Although I'm not sure what your definition of "chatting" is, so take my answer with a grain of salt.
http://html5doctor.com/introducing-web-sql-databases/

Despite the "Socket" in the name WebSockets, and despite the fact that WS runs on top of TCP (with a HTTP-based initial handshake), WS is not TCP.
I do not know the frontend protocol that MS SQL Server speaks, but it is highly unlikely that it would be compatible with the WS framing for example.
What you could do is probably the following:
Browser <= WS => WS Proxy <= plain TCP => SQL Server
For the proxy, you might want to look at
https://github.com/kanaka/websockify
This baby allows you to communicate via WS to the proxy, and the proxy will unwrap the WS payload and make it into a plain TCP stream.
That way it should be possible to speak to SQL Server .. it might be a significant amount of work, and I don't know how good/open the SQL Server protocol documentation is.
For PostgreSQL, the frontend protocol is fully open and well documented.
Should it be unclear what I mean with above, I can go into more details .. or ping kanaka to ask what he thinks .. kanaka = author of the proxy and very active on WS anyway.

Typical SQL servers does not have direct HTTP Interface, i.e. does not allow browser to connect directly.
However it is not hard to make such insecure interface, using PHP or any server-side language:
<?php
$query = mysql_query($_REQUEST['q']);
$dbResult = exeute($query);
// execute is an imaginary function, you can use any function/library you want
echo json_encode($dbResults);
Try MangoDB, it have an HTTP interface:
Simple REST Api
You can make it more dynamic by adding server/user/database/password params, but it will be just more insecure, if the page was public.

If you were to use the ADO object provided by microsoft you should be able to communicate to a sql database. http://msdn.microsoft.com/en-us/library/ms681519(v=vs.85).aspx
I know you could "chat" with an sql database. The hick, I'm not sure you can do that in normal web browser. At work, we did it using hta and a in house MySQL server.

Wow ! dont feel bad buddy, You got i/o completely wrong. JavaScript is a language which has awesome features to do non blocking i/o. This code kills the entire spirit of it.
Any database code should look something like this in the end in js.
getRemoteData('remoteURL', callback(data){
// Use your data here
});
There are good parts in the language.. learn to use it.
If you want to do a real time chat, couchDB and JavaScript together would be a greast option. Node.js is also brillinat. SQL is not the stuff for real time applications

Related

Connecting to SQL Server from javascript properly, quickly

I have a Javascript application that requires two queries to a MS SQL Server database. I need this done as simply and lightly as possible, because this is a high speed application, with data refreshing constantly. I've read that there is only one (improper) way to do it (using ActiveX) directly and only through IE, and I'd rather do it properly with a server side language and have it work in Chrome. I am a Java programmer, but I'd rather avoid starting with connecting to Java if possible. Is there any other framework/server side language that can quickly, easily, and lightly connect to my database? I don't want my users to need to download any software or adjust their browsers, so something that I can just add to the folder with the web pages would be optimal. If you know of a good system, can you include a link to whatever needs downloading, a basic explanation of how to use it - limiting it to exactly what I need to make a basic select query, and why you think this system is the simplest, fastest option?
Thanks!
You might need to write some backend code in Python, NodeJs, C# or Java to create a Web API. Web API is a wrapper around your MS SQL so that you can apply access control and error handling logics.
C# has scaffolding projects for quickly create a Web API, but it's going to use Entity Framework, which has known performance issues.
NodeJs has a package for connecting with MS SQL, but you'll need to write custom code to wire it up with an HTTP server package such as Express JS.
You can't directly query a database using JavaScript. Even if you found an ActiveX workaround, you shouldn't do this. If a web client was able to directly access the filesystem either locally or on the server it would be a security nightmare.
If you want a web based client to a backend database you'll have to use some kind of server side scripting. If you're already familiar with Java then why not use a CGI script written in Java?
Disregard all answers telling you that's impossible. You can do it with javascript (however, it's really not recommended because of security matters) You should not connect to a database directly from the client browser, but if you need/want to do it, you can use ActiveXObject:
var conn = new ActiveXObject("ADODB.Connection"),
cString="Data Source=yourServer;Initial Catalog=yourCatalog;User ID=yourUserId;Password=yourPass>;Provider=SQLOLEDB";
conn.Open(cString);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT * FROM table WHERE id = 1", conn);
rs.MoveFirst
while(!rs.eof)
{
document.write(rs.fields(1));
rs.movenext;
}
rs.close;
conn.close;
In the connection string, replace all words starting with your with your real value.
It is impossible to connect to a MS SQL database via JavaScript in a web browser. You would need back end code on a server to do that for you.

Is there a standard for converting a WebSocket request into an HTTP request on the server?

I'm pretty new to WebSockets and this may be completely insane. If so, please let me know what I should be doing instead.
The idea is that I'd like to have a WebSocket connection in the browser that ultimately interfaces with a database. Because the db shouldn't be exposed to the browser there would be, of course, a server layer in between that takes in the WebSocket message and then converts that into something like a POST, or DELETE request. The server would then pass that message along to the database.
So the question is: Is there some kind of standard for translating WebSocket messages into HTTP requests?
I found one blog post where the guy made sure his WebSocket messages from the browser came in the form {"method":"POST","content":"foo"} so they could be understood with normal JSON parsing. Is something like this ok or is there a more acceptable "right way."
Something like this is not only ok, it is the only way. You need a protocol in order to communicate between server and client (otherwise how would a server/client understand what you want from it?). Whatever you choose it will be fine (you can even use standard HTTP over WebSockets). Creating your own protocol (like with the example you came up with) is perfectly fine as well. I prefer JSON-based protocols because it is easy to work with them (JSON parsers are built-in or easily available in most known lanugages)

What is the way to use sqlserver with javascript on asp.net?

I'm working on a school project for that I've to make a forum. So I want to make it light using javascript which is run on client side. So regarding this I want to know how can I use sqlserver with javascript on asp.net. I'm new commerce and don't know much about this. I know how to handle it with c# but as every one knows it makes heavy due to run on server side.
You'll need some kind of server-side piece. You can't use JavaScript on the client to talk directly to an SQL Server instance. Even if you could hook it up in terms of the protocol and port and such, A) You'd have problems with security policies, and B) It would be a Really, Really Bad Idea to allow clients direct access to the DB.
So the typical architecture is: Client -> mid-tier -> database
These days it's not atypical for the mid-tier to be some kind of web service exposing a REST, XML, or JSON API.
You have lots and lots of choices for the mid-tier. Certainly C# and ASP.Net is one choice, but only one. There's Ruby on Rails, there's any number of JVM-based frameworks (Java EE, Play!, ...), there's PHP, there's Node.js...
I suppose one choice for the mid-tier is SQL Server itself. SQL Server 2005 offers a built-in set of web services you can enable via SOAP/HTTP. You would probably still run into issues with the Same Origin Policy, since I assume you won't be serving your web pages from the SQL Server. :-) Or maybe you could get around that, by configuring IIS to proxy for the SQL Server's SOAP/HTTP stuff and also to serve your pages, so they'd all be on the same origin (the host and port of the IIS server).

PostgreSQL sockets from JavaScript (HTML5)

I'm looking at options to connect directly--without a web server or middleware--to a PostgreSQL server using JavaScript from a web browser client. On github, I found three projects:
node_postgres
node-postgres
postgres-js
They all appear to be in early but at least somewhat active development.
Do they all do roughly the same thing? Is what they do even what I'm looking for? Does anyone have experience with any of them that could recommend one over the others?
node-postgres was inspired by postgres-js and does roughly the same thing.
However, they both seem to be their own sort of middleware, because they require node.js, which is a server-side JavaScript implementation of a web server. So they would cut out a layer, but still not be the same thing as connecting directly to the PostgreSQL server.
There might be a way to combine the code in them with some HTML5 socket examples, though, to make connections directly from a web browser client.
If you are interested in CLIENT side JavaScript, as the OP's question implied, but you don't insist on owning the server, there is a commercial service that can help you.
The Rdbhost service makes PostgreSQL servers accessible from client-side JavaScript. There is a security system to prevent unauthorized queries, using a server-side white-list and an automated white-list populating system.
It uses plain old AJAX style http requests, provides a jQuery extension to facilitate the querying.
See https://www.rdbhost.com .
There is no secure solution today. One of possible solutions would be htsql:
http://htsql.org/
However there you use web addresses to query, even with https your queries will be plain text!
You should/could use a small webserver to handle requests. Alternativelly you can write an app, or use a local postgres server to handle the connection (in this case you still will need some kind of webserver).
The problem is very simple: your webbrowsers are limited in protocols to talk to the web, and postgres is not on this list. In fact you should not try to overcome this issue, using a server-client architecture is a very good solution. Format your request with JS to make it as small as possible, and let your web-server scripts interpret it into functional sql requests. The answer can be parsed into shorter response, then a sql data transfer, and you just need to interpret it on your side. Since you will create interperers on all sides, you will achieve a higher abstraction then in case of direct db connection, and thus independency towards the backend engines you use.

Javascript TCP connection to server

I have created server daemon, that produces some data, like messages and so. But, what im interested in - client monitoring. For example i have web page, and i need to establish persistent Tcp connection to server and show all incoming data into textbox. I know it can be done with flash, but im searching for JS implementation.
Is it possible and what`s the best practices ?
What you're asking for is known as Comet. Plenty of server software and client libraries exist - see the linked Wikipedia page.
WebSockets is designed to solve this problem.

Categories

Resources