Socket.io + Express + Node + Angular notifications

I have an application in which I am trying to create a queuing system for clients. I try to make sure that when a customer joins the queue, he notifies store employees about the connection and lets them know in order to receive it and help the customer as soon as possible.

I can't get Socket IO to talk to my interface at all.

Here is the code I have, and I can't even get Node to recognize Socket as a library. I followed three different tutorials and tried them all, but I don’t see what is being done in my server code.

Here is what I do on my Socket.io server

var express = require('express'); var app = express(); var io = require('socket.io'); io.on('connection', function(socket){ socket.emit('connection', "Connection created.") console.log("Socket.io is GO"); socket.on('add customer', function(customer){ console.log("Customer added", customer); }) socket.on('notification', function(data) { console.log("NEW CUSTOMER IN THE QUEUE", data); }); }); 

I cannot get it to run on my server, and when it started, it did not show any of the socket.on events that I had there, and that would not do anything console.log.

What am I doing wrong? Has anyone successfully gotten Socket to play well with Express and Angular together?

+6
source share
2 answers

It looks like your code is incomplete or maybe you have a poor performance.

I created an example using angular.js, express and socket.io, trying to replicate your requirement.

This is my server, app.js :

 var express = require('express'); var app = express(); var server = require('http').Server(app); var io = require('socket.io')(server); app.use(express.static(__dirname + '/public')); io.on('connection', function(socket) { console.log('new connection'); socket.on('add-customer', function(customer) { io.emit('notification', { message: 'new customer', customer: customer }); }); }); server.listen(4041, function() { console.log('server up and running at 4041 port'); }); 

Then I created a shared folder with the following files:

public/index.html

 <!doctype> <html ng-app="sampleApp"> <head> </head> <body ng-controller="IndexController"> <label>Please enter your name: </label> <input ng-model="currentCustomer.name"/> <button ng-click="join()">Join</button> <br/> <h1>New Customers: </h1> <ul> <li ng-repeat="customer in newCustomers">{{customer.name}}</li> </ul> <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-route.js"></script> <script src="app.js"></script> </body> </html> 

public/app.js

 var app = angular.module('sampleApp', ['ngRoute']); app.config(['$routeProvider', function($routeProvider) { $routeProvider.when('/', { templateUrl: '/index.html' }); }]); app.factory('socket', ['$rootScope', function($rootScope) { var socket = io.connect(); return { on: function(eventName, callback){ socket.on(eventName, callback); }, emit: function(eventName, data) { socket.emit(eventName, data); } }; }]); app.controller('IndexController', function($scope, socket) { $scope.newCustomers = []; $scope.currentCustomer = {}; $scope.join = function() { socket.emit('add-customer', $scope.currentCustomer); }; socket.on('notification', function(data) { $scope.$apply(function () { $scope.newCustomers.push(data.customer); }); }); }); 
+11
source

You need to configure your socket.io server so that it listens on a specific port; without listening to it, it cannot monitor client connections and message distribution. You must change your code as follows to fix this:

 var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); //Existing Code server.listen(1337, function(){ console.log('listening on *:1337'); }); 
+1
source

Source: https://habr.com/ru/post/989706/


All Articles