Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose IP address and disconnect event #1674

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DRSDavidSoft
Copy link

@DRSDavidSoft DRSDavidSoft commented Mar 21, 2019

I'd like to be notified of when someone has connected to my session externally.

Summary

Since logConnections: true only emits a single Browser Connected: . . . line, bs has to expose the client's ip address from the socket event.

So, I am using the following code to be notified of new connections:

const nodeNotifier = require('node-notifier');
const bsUtils = require('browser-sync/dist/utils');
const emitter = browserSync.emitter;

emitter.on("client:connected", function ( data ) {
	let uaString = bsUtils.getUaString(data.ua);
	
	console.log(chalk`{green Connected:}`,
		`${uaString.name} version ${uaString.version}, IP: ${data.ip}`);

	// Additionally make a toast notification, if ( data.ip is external )
	nodeNotifier.notify({
		title: 'Browser connected',
		message: `${uaString.name} version ${uaString.version}, IP: ${data.ip}`,
		sound: false
	});

});

emitter.on("client:disconnected", function ( data ) {
	let uaString = bsUtils.getUaString(data.ua);
	
	console.log(chalk`{red Disconnected:}`,
		`${uaString.name} version ${uaString.version}`, chalk`{yellow IP:}`, `${data.ip}`);
});

Without exposing handshake.address and client.on("disconnect", { . . . }) methods, it's impossible to acquire the client's ip address and thus be notified of such events.

Scope

This PR exposes socket.io's following properties in order to write custom event listeners:

  • handshake.address
  • client.on("disconnect", { . . . })

Status

  • this PR does not seem to break any functionality.

Feedback

Would be glad to hear from you!

@DannyvanderJagt
Copy link

Any progress on this?

@DRSDavidSoft
Copy link
Author

@shakyShane I would appreciate it if you consider merging this! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants