10 examples of 'npm socket.io' in JavaScript

Every line of 'npm socket.io' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
29function open(port) {
30
31 // set default port
32 port = port || 8080;
33
34 // don't try to re-open
35 if (server.socket) {
36 return server.socket;
37 }
38
39 // update state
40 var socket = io(port);
41 server.socket = socket;
42
43 subscribe(socket);
44 return socket;
45}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

9function initSocket(io) {
10 state_1.State[safe.get('State._io')] = io;
11 io.on('connection', (socket) => {
12 const logger = new logger_1.Logger('ioServer');
13 state_1.State.pushSocket(socket);
14 logger.info(`socket connected: { socketId: ${socket.id}, pid: ${process.pid} }`);
15 logger.info('Implementing socket web services');
16 if (state_1.State.models && state_1.State.models.length) {
17 let models = state_1.State.models;
18 for (let i = 0; i < models.length; i++) {
19 let model = models[i];
20 if (model.$get('apiType') === types_1.API_TYPES.REST)
21 continue;
22 let services = model.$getServices();
23 for (let service in services) {
24 if (services[service].apiType === types_1.API_TYPES.REST)
25 continue;
26 socket.on(services[service].event, context_1.Context.Socket(socket, model, services[service]));
27 }
28 }
29 }
30 socket.on('disconnect', () => {
31 context_1.Context.deleteContextsOf('socket', socket.id);
32 state_1.State.deleteSocket(socket.id);
33 io.emit('user disconnected');
34 socket.disconnect();
35 });
36 });
37 process.on('message', msg => {
38 if (msg.data && msg.action === 'response') {
39 let response = msg.data;
40 if ((response.domain === types_1.RESPONSE_DOMAINS.ROOM || response.domain === types_1.RESPONSE_DOMAINS.ALL_ROOM) && response.room) {
41 io.to(response.room).emit(response.event, response);
42 }
43 else if (response.domain === types_1.RESPONSE_DOMAINS.OTHERS || response.domain === types_1.RESPONSE_DOMAINS.ALL) {
44 io.sockets.emit(response.event, response);
45 }
46 }
47 });
48}
3function SocketIO(){
4 console.log("I AM DOING A SOCKET")
5 //var dashboard = require('dashboard');
6 //var socket_io = require('libs/socket.io');
7 var dashboard = require('dashboard');
8
9 var io = require('socket.io');
10
11 var socket = null;
12
13 try{
14 socket = io();
15 } catch(ex){
16 console.error('connection to the engine via websocket failed : '+ ex.message);
17 }
18 if(socket!==null){
19 socket.on('connect', function() {
20 dashboard.ui.updateStatus();
21 });
22
23 socket.on('message',function(message){});
24
25 socket.on('status',function(status){
26 //disable the GET loop to refresh the status
27 try {
28 clearInterval(dashboard.ui.auto_refresh);
29 } catch(e) {}
30 dashboard.machine.status_report = status;
31 dashboard.updateStatus(status);
32 });
33
34 socket.on('disconnect', function() {
35 //reenable the GET loop to refresh the status
36 dashboard.ui.auto_refresh = setInterval(dashboard.ui.updateStatus.bind(dashboard.ui),dashboard.ui.refresh);
37 });
38 }
39
40 return socket;
41}
25function io() {
26 var mockSocketObject = createMockSocketObject();
27 return mockSocketObject.connect();
28}
4static init(io:Object):void {
5 io.on('connection', (client:Object) => {
6 client.on('users:online', () => {
7 io.emit('users:online', 1);
8 });
9 });
10}
9function init(server){
10 // socket.io
11 _socket = socket(server);
12 _socket.on('connection', function (socket) {
13 winston.log('info', 'a user connected');
14 });
15
16
17
18 return _socket;
19}
6function Socket(type, listener, host, io_options) {
7 events.EventEmitter.call(this);
8
9 //init state variables
10 this._listening = false;
11 this._binding = false;
12
13 //type of socket 'udp4', 'udp6', 'unix_socket'
14 this.type = type || 'udp4';
15
16 //listener
17 if (typeof listener === 'function')
18 this.on('message', listener);
19
20 //args swap
21 if(typeof listener === 'string') {
22 host = listener;
23 io_options = host;
24 }
25
26 io_options = io_options || {};
27
28 //alows muliple socket on one browser
29 io_options['force new connection'] = true;
30
31 //use sio manespaceing
32 host = (host || '') + '/simudp';
33
34 //connect socket.io
35 this.sio = io.connect(host, io_options);
36}
15connect() {
16 this._socket = io();
17
18 this._socket.on('connect', () => {
19 this.setupEventListeners();
20 });
21
22 return this._socket;
23}
6init(){
7 this.socket = io('localhost:3000', {jsonp: false});
8 this.state = { status: 'Not connected' };
9 this.socket.connect();
10 this.initListeners();
11 this.socket.emit('add user', 'React native');
12}
17function IO(fn) {
18 if (!(this instanceof IO)) {
19 return new IO(fn);
20 }
21 this.fn = fn;
22}

Related snippets