I just created a small socket.io chat and my server code is like
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
socket.broadcast.emit('Welcome to Dobuyme');
});
http.listen(port, function(){
console.log('listening on *:' + port);
});
An my client side in the index.html is
$(function () {
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
window.scrollTo(0, document.body.scrollHeight);
});
});
Now i can connect the chat server through the url
http://localhost:3000/
Now i want to integrate this module with my program, for that i need to pass a user id to this url.i just did something like
http://localhost:3000/index.html?id=2
But i cant pass the id.How can pass an id variable to node server through the url
Related
I am implementing a chat application using nodejs and socket.
Which is working perfect on localhost but not on live server(AWS). I had install socket, node and express js on server.
<script src="http://50.112.30.188/chatDemo/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
And in index.js file
var express = require('express'),
http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
console.log('listening on *:3000');
The error i am facing is
"http://50.112.30.188/socket.io/?EIO=3&transport=polling&t=1475061508132-0" not found as this is called continuously after running node from terminal with the cmd: node index.js.
i had also tried by add port in io() but makes no difference. So can any of can help me out from this problem as i am new in nodejs. That will be appricated.
Good morning why this code don't work (server respond but "io.on" no):
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//this code not work in console // even it can not see
io.on('connection', function(socket) {
console.log('a user connect');
socket.on('disconnect', function() {
console.log('a user disconnect');
});
});
//server respond
http.listen(3000, function() {
console.log('Server running at 3000');
});
I'm a beginner, express and socket.io i downloaded by npm
You need to have a server as well as a client which will make a connection to the socket io server.
Based on what I understood from your code, I guess you want to do something like this:
First your server.js:
#!/usr/bin/env node
var http = require('http');
var express = require('express');
var app = express();
var io = require('socket.io');
var port = 3000;
app.set('port', port);
var server = http.createServer(app);
server.listen(port);
var socketio = io.listen(server);
setInterval(function() {
console.log("Emitting..");
socketio.emit('data', {"x":"123"});
}, 2000);
module.exports = app;
Then your client.html: (I am writing a client, which will log into browser console.)
<html>
<head></head>
<body>
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect("http://localhost:3000/");
socket.on('connect', function() {
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
socket.on("data", function(data) {
console.log(data);
});
</script>
</body>
That's it. Now start your server with node server.js and then, open up client.html in your browser and check console. Let me know, if there's anything unclear.
I'm trying to use socket.io to send raspberry pi events from my express server. I can't seem to get my raspberry pi to connect to the server though. Any idea on what I'm doing wrong?
Rasberry Pi app:
var socket = require('socket.io-client')('192.168.0.10:3000');
console.log('running');
socket.on('connect', function(){
console.log('connected');
});
socket.on('event', function(data){
console.log(data);
});
socket.on('disconnect', function(){
console.log('disconnected');
});
Express Server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendfile('index.html');
});
io.emit('event', 'hello');
io.on('connection', function(socket){
console.log('a user connected');
});
io.on('disconnect', function(socket){
console.log('a user disconnected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Needed to change the first line of my Raspberry Pi to include http://
From :
var socket = require('socket.io-client')('192.168.0.10:3000');
TO:
var socket = require('socket.io-client')('http://192.168.0.10:3000');
I am trying to send a value with socket.io to a php file. I know that node.js will not interpret a PHP file.. I was just wondering if there any work around for this
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
app.get('/order_management.php', function (req, res) {
res.sendFile(__dirname + '/order_management.php');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
console.log('message: ' + msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
I am trying to sent a basic message when users connect and disconnect. Later i want to sent parameters back and forth. But with the following code i do not get the messages print out on the server when they connect/disconnect.
When i replace io.on with server.on i can get the message 'User connected' but not user disconnected when i refresh/close the page. This however is buggy, since not everytime i refresh it logs the message and on first time it sometimes log the message multiple times.
server.js
var //io = require('socket.io'),
http = require('http'),
express = require('express');
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
var app = express();
app.use(express.static(__dirname + '/views'));
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
});
server = http.Server(app);
//io = io.listen(server);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('User connected');
io.on('disconnect', function(socket) {
console.log('User Disconnected');
});
});
server.listen(PORT, HOST, null, function() {
console.log('Server listening on port %d in %s mode', this.address().port, app.settings.env);
});
New code:
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
server.listen(PORT);
app.use(express.static(__dirname + '/views')); //need this for my directory structure.
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
}); //Need this to call my page.
io.on('connection', function( client ){
console.log('Connected');//this does not work.
io.on('disconnect', function( client) {
console.log(client);
});
});
Try reconfiguring your socket, and server instantiation:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
io.on('connection', function( client ) {
console.log(client) //this will now work
client.on('disconnect', function( id ) {
console.log('user with ID ' + id + ' has disconnected');
});
});
First of all you need to be sure if you have included the client side socket.io files.
<script src="/socket.io/socket.io.js"></script>
Then you need to initialize the socket connection.
<script>
var socket = io();
</script>
The above changes will be in your index.html file.
Now in server.js file,
io.sockets.on('connection', function(socket) {
console.log("User connected");
socket.on('disconnect', function() {
console.log('Got disconnect!');
});
});