Skip to content

Instantly share code, notes, and snippets.

@j-n-c
Last active November 30, 2016 13:46
Show Gist options
  • Select an option

  • Save j-n-c/404df9b5b3a6c64ee1375a6846fce0f8 to your computer and use it in GitHub Desktop.

Select an option

Save j-n-c/404df9b5b3a6c64ee1375a6846fce0f8 to your computer and use it in GitHub Desktop.
- What is Socket.IO?
-- Socket.IO is an event-based bi-directional communication layer for realtime web applications, built atop Engine.IO that allows developers to send and receive data without worrying about cross-browser compatibility.
-- It abstracts many transports, including AJAX long-polling and WebSockets
-- It has two parts: a client-side library that runs in the browser, and a server-side library for node.js. Both components have a nearly identical API.
- SERVER SIDE:
-- Server()
Creates a new Server (works with and without new):
var socket_server = require('socket.io')();
// or
var Server = require('socket.io');
var socket_server = new Server();
//Choose port that server should listen to
var socket_port = 3001;
socket_server.on("connection", function(socket_event) {
var tweet = {user: "nodesource", text: "Hello, world!"};
// to make things interesting, have it send every second
var interval = setInterval(function () {
socket_event.emit("tweet", tweet);
}, 1000);
socket_event.on("disconnect", function () {
clearInterval(interval);
});
});
// or
var handleClient = function (socket_event) {
var tweet = {user: "nodesource", text: "Hello, world!"};
// to make things interesting, have it send every second
var interval = setInterval(function () {
socket_event.emit("tweet", tweet);
}, 1000);
socket_event.on("disconnect", function () {
clearInterval(interval);
});
};
socket_server.on("connection", handleClient);
server.listen(socket_port);
- CLIENT SIDE:
<script src="/socket.io/socket.io.js"></script>
<script>
//Choose a port for message exchange between client and server
var socket_port = 3001; //must be the same port configured on server-side
var socket_event = io.connect("http://localhost:" + socket_port);
//or
var socket_event = io.connect("http://" + window.location.hostname + ":" + socket_port);
socket_event.on("tweet", function(tweet) {
// todo: add the tweet as a DOM node
console.log("tweet from", tweet.user);
console.log("contents:", tweet.text);
});
</script>
- EMIT
-- socket_event.emit(<event_name>, <data_to_emit>): Send message only to the 'client' (either browser or webserver) who triggered the event
-- socket_event.broadcast.emit(<event_name>, <data_to_emit>): Send message to every connected 'client' (only available on webserver), except to the one who triggered the event
-- io.sockets.emit(<event_name>, <data_to_emit>): Send message to every connected 'client' (either browser or webserver). The 'client' who triggered the event will also receibe this message.
REFERENCES:
-- https://www.tutorialspoint.com/socket.io/socket.io_quick_guide.htm
-- http://www.programwitherik.com/socket-io-tutorial-with-node-js-and-express/
-- https://howtonode.org/websockets-socketio
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment