Write to Node.js socket - node.js

I'm trying to send a string from Node.js to a Java server but nothing will send from the Node.js client unless I call client.end() after. I'm not very experienced with Node.js so any suggestions would help.
var net = require('net');
var client = net.connect(1032, 'localhost')
client.on('connect', function(){
console.log('connected');
});
client.on('data', function(data){
console.log(data.toString());
client.write('test reply');
});
client.on('close', function(){
client.end();
});

var net = require('net');
var client = net.connect(1032, 'localhost')
client.on('connect', function(){
console.log('connected');
client.write('test reply');
});
client.on('data', function(data){
console.log(data.toString());
});
client.on('close', function(){
client.end();
});

Related

End of communication tcp server nodejs

Knowing that TCP is a stream based protocol, in the following example of a client server in nodejs what is telling the server that the client has finished sending all data?
Server
var net = require('net');
var server = net.createServer(function(socket) {
var remoteAddress = socket.remoteAddress + ':' + socket.remotePort;
console.log('new client connected: %s', remoteAddress);
socket.write('Echo server');
socket.pipe(socket);
});
server.listen(1337, '127.0.0.1');
client
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
for ( var i=0 ; i<100; i++) {
// console.log(i);
client.write('Hello, server! Love, Client.\r\n');
}
});
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy(); // kill client after server's response
});
client.on('close', function() {
console.log('Connection closed');
});
Thanks

Nodejs , Redis,Sockets

I have a nodejs app which reads data from redis and I am unable to push it into socket. In the c.write(message) part, if I hardcode(example c.write('hello') ,the messages are being put to the socket but when i put it as c.write(message), nothing is going to the socket. Thanks in advance,
var net = require('net');
var split = require('split');
var Redis = require('ioredis');
var redis = new Redis();
var server = net.createServer(function(c) {
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
redis.subscribe('test-channel');
redis.on('message', function(channel, message) {
console.log(message);
c.write(message);
c.pipe(c);
});
});
server.on('error', (err) => {
throw err;
});
server.listen(3005, 'localhost', () => {
console.log('server bound');
});
I have got the answer.
Just add below lines to your code :
var message_redis = message+'\r'+'\n';
c.write(message_redis);

Socket.io Client Side socket.io#0.9.16

My problem is that I can connect to my client via html/js but I cannot do the same thing using node. I am using socket.io#0.9.16 .
var io = require('socket.io');
var socket = io.connect('https://website.com:3000');
socket.on('connect', function (data) {
socket.emit('room', 'connecting');
})
socket.on('message' , function (data){
console.log("message has been sent");
});
vs
<html>
<script src="https://website.com:3000/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io.connect('https://website.com:3000');
socket.on('connect', function (data) {
socket.emit('room', 'connecting');
})
socket.on('message' , function (data){
alert(data.message);
});
</script>
</html>
npm install socket.io-client#0.9.16
var io = require('socket.io-client');
var socket = io.connect('https://website:3000') ;
socket.on('connect', function (data) {
socket.emit('room', 'foo');
console.log('connecting... working...')
});
socket.on('someRoom' , function (data){
console.log('update ...');
console.log(data);
});
You don't want socket.io, you need to use socket.io-client.
var socket = require('socket.io-client')('https://website.com:3000');
socket.on('connect', function(){
socket.emit('room', 'connecting');
socket.on('message' , function (data){
alert(data.message);
});
});

Node.js socket.io server callback not working

I'm working on a simple Node.js bi-directional client\server communication channel and I'm attempting to use socket.io on the server and socket.io-client on the client.
I've inserted the code below, as you'll see it's pretty basic stuff, and I'm running both on my local machine - to minimise complexity.
The behaviour I'm seeing is:
I start the server.
The server logs 'Server started' to the console.
I start the client.
The client logs 'Server is ready!'
Nothing else happens...
What I'd expect is the server to log a 'Client is ready!' message and then the content ('Ready received').
I've even used WireShark to sniff the line and it does appear that the client is emitting the message, as designed - but the callback on the server isn't firing.
I'm running node v0.8.4 with express v3.1.0, socket.io v0.9.13 and socket.io-client v0.9.11 (all installed via npm).
Here's the server code...
var http = require('http'),
express = require('express'),
app = express(),
server = http.createServer(app);
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
server.listen(8080);
console.log("Server started");
var io = require('socket.io').listen(server);
io.sockets
.on('connection', function(socket){
socket.emit('server ready', { msg: 'ready' }) ;
})
.on('comms', function(content) {
console.log('Client is ready!');
console.log(content);
});
And here's the client code...
var clientio = require('socket.io-client');
var socket = new clientio.connect('http://localhost', { port: 8080 });
socket
.on('server ready', function(data){
console.log('Server is ready!');
socket.emit('comms', 'Ready received');
})
.on('connect-error', function(error) {
console.log('Connection Error\n' + error);
})
.on('error', function(error) {
console.log('Socket Error\n' + error);
})
The documentation and examples for both socket.io and socket.io-client are somewhat confused (to be charitable) and they appear to be a bit of a moving target... but from what I can tell, I think this should work.
I'm hoping someone can give me advice as to where I'm going wrong?
In your server you have this code:
io.sockets
.on('connection', function(socket){
socket.emit('server ready', { msg: 'ready' }) ;
})
.on('comms', function(content) {
console.log('Client is ready!');
console.log(content);
});
What you should do is something like this:
io.sockets.on('connection', function(socket){
socket.emit('server ready', { msg: 'ready' });
socket.on('comm', function(content){
console.log('Client is ready!');
console.log(content);
});
});
hopefully this is doing more or less what you need it to do. Just a couple of minor changes.
app.js
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
// using 'connect' to handle static pages
app.use(require('connect').static(__dirname + '/public'))
server.listen(8080);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('server ready', { msg: 'ready' });
socket.on('comms', function(content) {
console.log(('Client is ready\n'));
console.log(content);
});
});
index.html
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
window.jQuery || document.write('<script src="js/jquery-1.8.3.min.js"><\/script>')
</script>
<script src="/socket.io/socket.io.js"></script>
</head>
<body>
<script>
(function (d, b) {
function bindEvents() {
function doSomething(msg) {
$.each(msg, function (key, value) {
console.log('doSomething...');
$("body").append("<p>" + value + "</p>")
});
};
// var socket = io.connect();
var socket = io.connect('http://localhost');
socket.on('server ready', function (msg) {
console.log('Server is ready!\n', msg);
doSomething(msg);
socket.emit('comms', {
msg: 'Client is Ready'
});
});
socket.on('connect-error', function (err) {
console.log('Connection Error\n', err);
});
socket.on('error', function (err) {
console.log('Connection Error\n', err);
});
};
$(document).ready(function () {
bindEvents()
});
})(jQuery, this)
</script>

node.js + socket.io example: client connects but socket.send() from client to server fails

server code
var http = require('http'),
io = require('socket.io'),
fs = require('fs');
respcont = fs.readFileSync('socketio.client.js');
server = http.createServer(function(req, res){
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(respcont);
});
server.listen(8080);
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ console.log('message arrived'); })
client.on('disconnect', function(){ console.log('disconnected'); })
});
client code
<html>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8080,rememberTransport:true,timeout:1500});
socket.connect();
socket.on('connect', function(){ console.log('connected to server'); socket.send('hi there, this is a test message'); })
socket.on('message', function(){ console.log('recieved a message!'); })
socket.on('disconnect', function(){ console.log('disconnected from server'); })
</script>
</body>
</html>
looks like node.js HEAD was broken.
git pull to 0474ce67908c9afddab69d3f0eb53564b10e2ad1 fixed the problem.

Resources