Code Monkey home page Code Monkey logo

socket-notification's People

Contributors

shaikh-shahid avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

socket-notification's Issues

Handling socket notification from the node.js server

This is not an issue per-se but just a question on the variation of how to handle the notification from the server.

Here is my case:

I am inserting a record using mysql upon which I need to send notification to other connected users.

This is the code:

app.js

var http = require('http');
var io = require('socket.io')(http);

io.on('connection', function(socket){
	console.log('a user connected');
	socket.on('new-rec-created', function(){
		//io.emit('a new housekeeping was requested'); // for all
		socket.broadcast.emit('notify everyone'); // for all except me
	});
});

route.js

var http = require('http');
var io = require('socket.io')(http);

connectionPool.getConnection(function(err, connection){
	if(err) {
		winston.log('debug', '------------------------------------ Error: ' + err);
		connection.release();
		return;
	}
	connection.query(queryString, function(err, rows){
		if (err) {
			winston.log('debug', '------------------------------------ Error: ' + err);
		}
		connection.release();
		socket.emit('new-rec-created');
	});
});

my public side main.js file

$(document).ready(function(){
	var socket = io();
	socket.on('notify everyone',function(){
		notifyMe(); //the js desktop notification code
	});
	.
	.
});

Couple of issues I am facing:

  1. In the html when I use

<script src="/socket.io/socket.io.js"></script> it kept giving me 404 not found error. So finally I had to use the cdn <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.1/socket.io.js"></script>

  1. In the router it gives me an error saying socket is undefined. If I do io.socket.emit(..) it says Cannot read property emit of undefined.

I dont understand what I am doing wrong. The weird thing is when I start the server the console.log('a user connected') under io.on('connection') is not printed. Any suggestions or advice?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.