admin管理员组文章数量:1317894
I want to send messages from my server to my client when a function is called. Using the code from this answer messages can be successfully sent from Server to Client every second.
I am building an application that runs node in the background, ideally I would like to be able to click a button that will call a function in the node server.js file which takes a parameter and sends that message to the client. The function in question would look like this
function sendToClient(message) {
clients[0].emit('foo', msg);
}
This would send the passed in message to the first client. How can I go about this?
In terminal, after you run node server.js
is there a way to call a function from the server file using terminal, this could be a possible solution if so.
I want to send messages from my server to my client when a function is called. Using the code from this answer messages can be successfully sent from Server to Client every second.
I am building an application that runs node in the background, ideally I would like to be able to click a button that will call a function in the node server.js file which takes a parameter and sends that message to the client. The function in question would look like this
function sendToClient(message) {
clients[0].emit('foo', msg);
}
This would send the passed in message to the first client. How can I go about this?
In terminal, after you run node server.js
is there a way to call a function from the server file using terminal, this could be a possible solution if so.
- 3 I believe that you are referring to web sockets. Read up on socket.io and then try it out yourself. It's pretty straight forward. – KJ Price Commented Mar 9, 2015 at 21:49
- @KJPrice I use Socket.io in my actual project, my client is written in HTML and I am able to call JavaScript functions through my C# Application to send messages to the Server, that part is straight forward, I haven't however been able to do the same from Server to Client – Marcus Commented Mar 9, 2015 at 22:02
- I guess this is similar to what you were looking for: url. Did you take a look ? – Arnaud Bertrand Commented Mar 9, 2015 at 22:09
- @ArnaudBertrand Yes I did, but as I mentioned actually sending a message isn't the problem, I want to be able to call a function in the server.js file, pass in a parameter and then send that message from Server to Client. This should only happen when the function is called – Marcus Commented Mar 9, 2015 at 22:15
- By taking the same variables as the example in the page if you do ioClient.emit('nameyouwant',variable) you can call a function in your server side by doing on your server socket.on('nameyouwant', function(var){}). This is client -> server. If this it what you are looking for I can make it more clear in an answer. – Arnaud Bertrand Commented Mar 9, 2015 at 22:31
3 Answers
Reset to default 5The best way to send messages from server to client right now is using webSockets. The basic concept is this:
- Client A loads web page from server B.
- Client A runs some javascript that creates a webSocket connection to server B.
- Server B accepts that webSocket connection and the socket stays open for the duration of the life of the web page.
- Server B registers event handlers to handle ining messages from the web page.
- Client A registers event handlers to handle ining messages from the server.
- At any point in time, the server can proactively send data to the client page and it will receive that data.
- At any point in time, the client may sent data to the server and it will receive that data.
A popular node.js library that makes webSocket support pretty easy is socket.io
. It has both client and server support so you can use the same library for both ends of the connection. The socket.io library supports the .emit()
method mentioned in your question for sending a message over an active webSocket connection.
You don't directly call functions from client to server. Instead, you send a message that triggers the server to run some particular code or vice versa. This is cooperative programming where the remote end has to be coded to support what you're asking it to do so you can send it a message and some optional data to go with the message and then it can receive that message and data and execute some code with that.
So, suppose you wanted the server to tell the client anytime a temperature changed so that the client could display in their web page the updated temperature (I actually have a Raspberry Pi node.js server that does exactly this). In this case, the client web page establishes a webSocket connection to the server when the page loads. Meanwhile, the server has its own process that is monitoring temperature changes. When it sees that the temperature has changed some meaningful amount, it sends a temperature change message to each connected client with the new temperature data. The client receives that message and data and then uses that to update it's UI to show the new temperature value.
The transaction could go the other way too. The client could have a matrix of information that it wants the server to carry out some plicated calculation on. It would send a message to the server with the type of calculation indicated in the message type and then send the matrix as the data for the message. The server would receive that message, see that this is a request to do a particular type of calculation on some data, it would then call the appropriate server-side function and pass it the client data. When the result was finished on the server, it would send a message back to the client with the result. The client would receive that result and then do whatever it needed to with the calculated result.
Note, if the transactions are only from client to server with a response then ing back from the server, a webSocket is not needed for that type of transaction. That can be done with just an Ajax call. Client makes ajax call to the server, server formulates a response and returns the response. Where webSockets are most uniquely useful is if you want to initiate the munication from the server and send unsolicited data to the client at a time that the server decides. For that, you need some continuous connection between client and server which is what a webSocket is designed to be.
It appears there may be more to your question about how to municate from a C# server to your node.js server so it can then notify the client. If this is the case, then since the node.js server is already a web server, I'd just add a route to the node.js server so you can simply do an http request from the C# server to the node.js server to pass some data to the node.js server which it can then use to notify the appropriate client via the above-described webSocket connection. Depending upon your security needs, you may want to implement some level of security so that the http request can only be sent locally from your C# server, not from the outside world to your node.js server.
In order to send a mand to a client via the console there are two options, single process or multiprocess:
Single Process
- When the mand is run from console, temporary
socket.io
server starts listening on a port. - Once the client connects, send the message to the client.
- Disconnect and stop the console app.
The reason this works is that socket.io
clients are always trying to connect to the server. As long as the browser is open, they will try to connect. So even if the server only es on for a few seconds, it should connect and receive messages. If the client is not running then simply create a timeout that will stop the console app and inform the user that it failed to broadcast the mand.
While this approach is very easy, it's not robust nor efficient. For small projects this would work, but you'll have better luck with the next approach:
Multi-Process
This approach is much more reliable, expandable, and just better looking when you are talking about architecture. Here's the basic summary:
- Spin up a stand-alone server that connects with clients.
- Create a very similar console
node
app that will send a message to the server to forward on to clients. - Console app pletes but the main server stays up and running.
This technique is just interprocess munication. Luckily you already have Socket.IO on the primary server, so your console app just needs to be another socket.io
client. Check out this answer on how to implement that.
The downside to this is that you must secure that socket munication. Maybe you can enforce it to just allow localhost
connections, that way you need access to the server to send the run mand
message (you absolutely don't want web clients executing code on other web clients).
Overall it es down to the needs of your project. If this is a quick little experiment you want to try out, then just do it single process. But if will be hosting an express server (other webservers are available) and need to be running anyways, then multi-process is the way to go!
Example
I've created a simple example of this process using only Socket.io. Instructions to run it all are in the readme.
Implementations
In order to have C# (app
) -> Node.js (server
) -> Browser (client
) munication then I would do one of the following:
- Use redis as a message queue (add items to the queue with the
app
, consume with theserver
, which sends mands toclient
). - Live on the wild side and merge your NodeJS and C# runtimes with Edge.js. If you can run NodeJS from C# you will be able to send messages from your
app
to theserver
(messages are then handled by theserver
, just like any othersocket.io
client-server model). - Easier, but still kinda hacky, use
System.Diagnostics.Process
to start a console tool explained in the Multi-Process section. This would simply run the process with arbitrary parameters. Not very robust but worth considering, simple means harder to break (And again, messages are then handled by theserver
, just like any othersocket.io
client-server model).
I would create a route for sending the message and send message from post parameter. From CLI you can use curl or from anywhere really:
app.get('/create', function(req, res) {
if( data.type && data.content && data.listeners){
notify( data );
}
});
var notify = function( notification ){
ns_mynamespace.in(notification.listeners.users)
.emit("notification", {
id: notification.id,
title: 'hello', text: notification.content });
}
}
本文标签: javascriptSend message from Nodejs Server to Client via function callStack Overflow
版权声明:本文标题:javascript - Send message from Node.js Server to Client via function call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742037529a2417383.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论