TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: A Unixy approach to WebSockets

421 点作者 joewalnes超过 10 年前

30 条评论

jeremiep超过 10 年前
&gt; Write programs to handle text streams, because that is a universal interface.<p>I always hated that part of UNIX. It would be so much better if programs could handle data-structure streams instead. Having text streams causes every single program to implement ad-hoc parsers and serializers.<p>I now use the command-line only for trivial one-time actions. For everything else I&#x27;ll use a scripting language and forget about reading and writing text streams altogether.
评论 #9052100 未加载
评论 #9051644 未加载
评论 #9051480 未加载
评论 #9051470 未加载
评论 #9051578 未加载
评论 #9051557 未加载
评论 #9052736 未加载
评论 #9053337 未加载
评论 #9051901 未加载
评论 #9051976 未加载
joewalnes超过 10 年前
OP here. Something I forgot to mention on the page is how much this simplified life for a server admin. Using &quot;ps&quot; you can see each connection, the memory overhead, CPU usage. It&#x27;s easy for a sysadmin to kill bad connections without taking down the whole server.
评论 #9051307 未加载
评论 #9051821 未加载
elierotenberg超过 10 年前
Unless you need to handle a very small number of concurrent connections, using 1 process per connection seems to be a huge overhead, although I can think of some use cases.<p>However, I can imagine a similar tool doing multi&#x2F;demultiplexing, eg the handler process would take input text lines prepended with a connection identifier (eg. &quot;$socketID $message&quot;) and output using a similar formatting. Pretty much like websocketd but with multiplexing and unixy&#x2F;pipe-friendly (eg. you can apply grep&#x2F;awk&#x2F;etc before and after).<p>How would this fit compared to websocketd?
评论 #9051515 未加载
评论 #9051234 未加载
评论 #9052024 未加载
评论 #9051622 未加载
Lerc超过 10 年前
I used this a while ago on my own insane project. I had a Webdav server and websocketd providing a Web interface to a Linux box.<p>Even at it&#x27;s young age (bug reports #5 and #7 were mine), it allowed me to progress a lot further in my project before I needed to write a server designed specifically for the task at hand.<p>In the end I wrote userserv <a href="https://github.com/Lerc/userserv" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Lerc&#x2F;userserv</a> for the one must have feature I needed. I needed logins and responses delivered from a process with the UID of the CookieToken.<p>So, thanks Joe. Notanos got further because of websocketd and while I&#x27;m not currently using it, There&#x27;s a high chance of me doing so in future projects.
评论 #9052806 未加载
riobard超过 10 年前
I guess the next logical step is to build a virtual filesystem such that each connection is represented by a file. Then you can further decouple the connection handling from the actual application. Applications can start later to talk to a connection established before.
评论 #9051514 未加载
评论 #9052194 未加载
评论 #9051982 未加载
ianbicking超过 10 年前
I don&#x27;t understand how I&#x27;d use this. WebSockets are generally for server-initiated events, but this doesn&#x27;t make it very easy to initiate events on the server. Usually it will be some pubsub situation, so I&#x27;d want a server process to be able to emit a message to one or many connections - but instead the connection is tied to one process for its life. I&#x27;d like to see an example like a simple chat room.
评论 #9052053 未加载
评论 #9052827 未加载
评论 #9052323 未加载
NathanOsullivan超过 10 年前
A project in a similar vein is websockify ( <a href="https://github.com/kanaka/websockify" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kanaka&#x2F;websockify</a> ) , which makes an existing app with a TCP endpoint available over websockets.
评论 #9051089 未加载
评论 #9051033 未加载
kragen超过 10 年前
&gt; Each inbound WebSocket connection runs your program in a dedicated process. Connections are isolated by process.<p>That sounds bad; it <i>is</i> like “CGI, twenty years later”, as they say. In 2000 at KnowNow, we were able to support over ten thousand concurrent Comet connections using a hacked-up version of thttpd, on a 1GHz CPU with 1GiB of RAM. I’ll be surprised if you can support ten thousand Comet connections using WebSockets and websocketd even on a modern machine, say, with a quad-core 3GHz CPU and 32GiB of RAM.<p>Why would you want ten thousand concurrent connections? Well, normal non-Comet HTTP is pretty amazingly lightweight on the server side, due to REST. Taking an extreme example, this HN discussion page takes 5 requests to load, which takes about a second, but much of that is network latency — a total of maybe ½s of time on the server side. But it contains 7000 words to read, which takes about 2048 seconds. So a single process or thread on the server can handle about 4096 concurrent HN readers. So a relatively normal machine can handle hundreds of thousands of concurrent users without breaking a sweat.<p>On the other hand, Linux <i>has</i> gotten a <i>lot</i> better since 2000 at managing large numbers of runnable processes and doing things like fork and exit. httpdito (<a href="http://canonical.org/~kragen/sw/dev3/server.s" rel="nofollow">http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;sw&#x2F;dev3&#x2F;server.s</a>) can handle tens of thousands of hits on a single machine nowadays, even though each hit forks a new child process (which then exits). <a href="http://canonical.org/~kragen/sw/dev3/httpdito-readme" rel="nofollow">http:&#x2F;&#x2F;canonical.org&#x2F;~kragen&#x2F;sw&#x2F;dev3&#x2F;httpdito-readme</a> has more performance notes.<p>On the gripping hand, httpdito’s virtual memory size is up to 16kiB, so Linux may be able to handle httpdito processes better than regular processes.
评论 #9059334 未加载
detaro超过 10 年前
Looks very nice for quick prototype kind of things in any language or to make existing tools quickly available to a small circle of users!<p>for larger-scale usage the overhead probably is to big.
评论 #9051026 未加载
评论 #9051039 未加载
vram22超过 10 年前
I had come across websocketd a while ago, and to try it out, wrote a simple Python program and a web page. The two posts about it are here:<p>1. Use WebSockets and Python for web-based system monitoring:<p><a href="http://jugad2.blogspot.in/2014/01/use-websockets-and-python-for-web-based.html" rel="nofollow">http:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2014&#x2F;01&#x2F;use-websockets-and-python-...</a><p>2. websocketd and Python for system monitoring - the JavaScript WebSocket client:<p><a href="http://jugad2.blogspot.in/2014/01/websocketd-and-python-for-system.html" rel="nofollow">http:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2014&#x2F;01&#x2F;websocketd-and-python-for-...</a><p>Note: As it says in one of the posts, you have to:<p>set PYTHONUNBUFFERED=true<p>for the Python program to work as a websocket server, though it works fine without that if only run directly at the command line (without websocketd). Thanks to Joe for pointing this out.<p>websocketd is a nice utility.
Udo超过 10 年前
This is a good idea.<p>Myself, I noticed that almost all my websockets projects could easily share one single code base, and finally I just made a Websockets boilerplate repo that I can pull from for any given project. This is what node.js really excels at, and it&#x27;s an execution model fundamentally different from websocketd: being a message broker between the client and the request-based web server.<p>Some people don&#x27;t like the separation between web server and websockets server, but when you think about it they don&#x27;t belong quite on the same level of abstraction. Plus, it&#x27;s usually orders of magnitude easier to reason about single requests than to reason about a complex persistent application server&#x27;s state.
lordlarm超过 10 年前
For simple one-directional communication (Server to client(s)) as shown in the example, it may for many people be simpler to use EventSource [1]<p>I&#x27;ve used this for UI&#x27;s where the server continuously sends&#x2F;pushes updates to the clients. Really handy, and multiple implementations and libraries available in most languages.<p>Of interest is perhaps also the spec [2]<p>[1]: <a href="https://developer.mozilla.org/en-US/docs/Web/API/EventSource" rel="nofollow">https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;EventSource</a><p>[2]: <a href="https://html.spec.whatwg.org/multipage/comms.html#the-eventsource-interface" rel="nofollow">https:&#x2F;&#x2F;html.spec.whatwg.org&#x2F;multipage&#x2F;comms.html#the-events...</a>
评论 #9051254 未加载
minimax超过 10 年前
The thing with WebSockets is that they are message oriented. WebSockets endpoints are presented with a series of distinct messages, whereas stdin&#x2F;stdout are stream based and you have to build messaging on top, if that&#x27;s what you want. I guess the idea here is that you are just using &#x27;\n&#x27; as the message delimiter?
评论 #9051212 未加载
jkarneges超过 10 年前
Elegant approach. Reminds me of inetd.
评论 #9051980 未加载
romanpoet超过 10 年前
Why is this better than netcat?<p><a href="https://en.wikipedia.org/wiki/Netcat" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Netcat</a>
评论 #9051977 未加载
keyle超过 10 年前
Oh my! I have been waiting for this, trying to write my own websocket server implementation in multiple esoteric languages! Fantastic!
aabajian超过 10 年前
This looks great, without knowing much I was able to get:<p>.&#x2F;websocketd --staticdir=. --port=8123 ps<p>to give me a simple output of processes. What I&#x27;d really LOVE is to get this to work:<p>.&#x2F;websocketd --staticdir=. --port=8123 htop<p>It&#x27;d be great if I could see the output of htop on the web...from anywhere. I guess htop is setting up a different video mode or something that isn&#x27;t compatible?
评论 #9051999 未加载
runewell超过 10 年前
This is perfect for admin tools. I&#x27;d rather parse stdout strings than code a custom API.
frknbasaran超过 10 年前
I&#x27;ll try this. Looks good!
angersock超过 10 年前
Oh, this is going to be so fun for doing terrible things.
评论 #9051284 未加载
hurin超过 10 年前
Could anyone give an example of what this is used for?
评论 #9052839 未加载
评论 #9051860 未加载
dkarapetyan超过 10 年前
This is cool. I can see this being used for prototyping and validating an idea before going full steam ahead with a more scalable framework.
MatthewWilkes超过 10 年前
It&#x27;s a petty comment, I know, but:<p><pre><code> for count in range(0, 10): print count + 1 sleep(0.5) </code></pre> makes me feel sad.
评论 #9057113 未加载
spiralpolitik超过 10 年前
I assume you are treating `stdin` and `stdout` as just a stream of bytes rather than trying to do anything clever a la Python 3 ?
评论 #9052890 未加载
cypher543超过 10 年前
&gt; &quot;Write programs that do one thing and do it well.&quot;<p>So why does this WebSocket daemon also serve static files and CGI applications?
评论 #9052146 未加载
tomerbd超过 10 年前
beautiful website may I ask with which template &#x2F; framework it was written? (is it in github by any chance?)
评论 #9052852 未加载
nichochar超过 10 年前
Holy shit that&#x27;s cool
kalmi10超过 10 年前
How are message boundaries handled?<p>WebSockets are message-based. UNIX streams are not.
评论 #9054194 未加载
评论 #9054195 未加载
nubb超过 10 年前
Link is down =(
curiously超过 10 年前
this is <i>fucking</i> cool. I got it instantly just glancing at the code example. I immediately understood how it worked. That is powerful stuff.<p>My only worry of course is, how would you scale this up? What&#x27;s really going beneath the hood.<p>I&#x27;m really excited and trying to think of something so I can use it as an excuse to use this.<p>The only other suggestion I would make is maybe change the name to something more catchy and brandable. Websocketd...okay like systemd...but I don&#x27;t know, something as good as this deserves a brandable name like Jupiter, or some Greek goddess or clever hacky name.
评论 #9052830 未加载