Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33510
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Executing code pushed from the server |
| Date | 2017-06-09 04:54 +0200 |
| Organization | albasani.net |
| Message-ID | <ohd2lm$o1i$1@news.albasani.net> (permalink) |
| References | <769e8d95-b693-4d1c-ae54-07f273d1c331@googlegroups.com> <ogn1qo$i9i$1@news.albasani.net> <ed64dd4c-30c1-46bc-9158-2e30c6334d75@googlegroups.com> |
bit-naughty@hotmail.com wrote:
> On Wednesday, May 31, 2017 at 11:55:32 PM UTC+5:30, Stefan Weiss wrote:
>
>>> Is there any way some JS code can be SENT TO the client, upon a keypress
>>> at the server, and executed there? Like, people are on a website, and when
>>> the key is pressed on the server, they see something new happening inside
>>> their browser?
>>> I'm guessing this is something to do with EventSource and eval,
>>> but....*how* though?
>>
>> It's possible, but before we discuss *how*, I'd really like to know *why*.
>> A person manually operating a web server and selectively sending out bits of
>> code is, to put it mildly, an unusual setup.
>
> Hehe it IS! :) I'm thinking of kind of.... the Unix console, you know,
> when you shutdown it tells EVERYBODY'S terminals - "Shutting down now" or
> whatever - I kinda wanted to do that, web wise :) That's text of course, but
> you get the idea....
I get the idea. I think I'm still missing the point of it, how it could be
useful in practice, but lets put that aside now.
It's been a very long time since I saw an unexpected wall(1) message in a
terminal, and I have my doubts if this is really something we want to port
from Unix to web applications. When it happened, chances were good you were
busy editing a file and then it just WROTE OVER whatever you had on your
display. Not fun.
Server outage/maintainence announcements for web applications are typically
not done in the way you describe - "hey everybody, server's going down NOW!"
- but rather well in advance. Hours/days/weeks, depending on the
application. Not only does this give the users enough time to finish their
tasks, it also removes the requirement of immediate communication (press a
key on the server). You can just add the notification in whatever component
builds the HTML, and it will be displayed the next time the user loads a page.
Simplified pseudo code:
<.... html ....>
<? if file "admin-announcement.html" exists ?>
<? include file "admin-announcement.html" ?>
<? end if ?>
<.... html ....>
That could be all there is to it. Depending on your interface, you may
already have a way to notify users of events that require their attention.
For example, the time tracking software I use will have a maintainance
outage next Saturday, so they made the bell icon in the sidebar noticably
active earlier this week, and they added an animated "new messages" counter,
similar to what you see on social media when you're notified about a new
message.
http://i.imgur.com/OmGCX9g.png
(this is after I read the announcement)
There are special situations where users may not load new pages for a long
time and a different approach is needed. Examples for this would be online
games where users stay on the same page for hours, or single-page web
applications that only communicate with the server via XHR. But in all those
scenarios you already have a communication channel with the users; all you
need to do is add support for the display of admin announcements on the client.
If you really really want to communicate with your users directly, at the
press of a key, and you do not already have an active communication channel,
your best options are polling (as Evertjan suggested) or WebSockets (as
Richard Maher suggested). Both use up resources on the client and the
server, which is why I would strongly recommend against doing this unless
you have a better reason than "I kinda wanted to do that".
Both options are also much more complex than (I think) you imagine them. You
need something on the server to register your key press. What that is
depends very much on your setup. Is it a server (instance) you rented
somewhere? Are you actually sitting in the server room with a keyboard
attached to the box? Does the server have a graphical UI running? Which OS
is it running?
Maybe the simplest solution would be to use an existing chat client. Every
time the user loads a page, they (re-)join the chat in the background. An
administrator can also join the chat (using any old browser, and not
necessarily on the server) and send messages. The admin messages are then
displayed on every user's screen.
I'm not saying you should do it that way. It's a waste of resources for
little benefit, but it can do what you asked, and it's relatively easy to
implement.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Executing code pushed from the server bit-naughty@hotmail.com - 2017-05-31 10:34 -0700
Re: Executing code pushed from the server "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-05-31 19:43 +0200
Re: Executing code pushed from the server Stefan Weiss <krewecherl@gmail.com> - 2017-05-31 20:25 +0200
Re: Executing code pushed from the server bit-naughty@hotmail.com - 2017-06-01 09:35 -0700
Re: Executing code pushed from the server Stefan Weiss <krewecherl@gmail.com> - 2017-06-09 04:54 +0200
Re: Executing code pushed from the server Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2017-06-01 06:39 +0800
Re: Executing code pushed from the server bit-naughty@hotmail.com - 2017-06-09 08:54 -0700
Re: Executing code pushed from the server Stefan Weiss <krewecherl@gmail.com> - 2017-06-10 02:13 +0200
csiph-web