Answers for "how to use socket.io"

2

install socket.io

npm i socket.io
Posted by: Guest on October-03-2020
2

socket io script

<script src="/socket.io/socket.io.js"></script>
<script>
  const socket = io();
</script>
Posted by: Guest on August-29-2020
0

create a web server node js w socket.io

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
  // Ran when a GET request to path '/'
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
  // Ran when a socket connected
});

http.listen(3000, () => {
  // Ran when server is ready to take requestes
});
Posted by: Guest on December-31-2020
3

socket..io

<script src="https://cdn.socket.io/socket.io-3.0.1.min.js"></script>
Posted by: Guest on November-24-2020
1

js socket.emit

const socket = io('ws://localhost:3000');
socket.on('connect', () => {  
  // either with send()  
  socket.send('Hello!');
  // or with emit() and custom event names  
  socket.emit('salutations', 'Hello!', { 'mr': 'john' }, Uint8Array.from([1, 2, 3, 4]));});
// handle the event sent with socket.send()
socket.on('message', data => {
  console.log(data);
});
// handle the event sent with socket.emit()
socket.on('greetings', (elem1, elem2, elem3) => {
  console.log(elem1, elem2, elem3);
});
Posted by: Guest on September-21-2020
1

how to run socket.io server

const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const port = process.env.PORT || 8001;
const index = require("./routes/index");
const app = express();
app.use(index);
const server = http.createServer(app);
const io = socketIo(server); // < Interesting!
const getApiAndEmit = "TODO";
Posted by: Guest on November-21-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language