SwiftUI 2.0 + Socket.io: Socket between an iOS Client and Node.js Server (2020)

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ต.ค. 2024
  • In this tutorial we will use socket.io to establish a socket communication between a node.js server and an iOS Client.
    Links mentioned in the video:
    Homebrew:
    brew.sh
    Socket iOS Client Framework:
    github.com/soc...

ความคิดเห็น • 47

  • @BeyondOnesAndZeros
    @BeyondOnesAndZeros  3 ปีที่แล้ว +10

    With the new socket.io version the api changed a bit. You should see the following error if you follow the code in the video:
    TypeError: require(...).listen is not a function
    To fix this simply replace this line:
    var io = require('socket.io').listen(server);
    With:
    var io = require('socket.io')(server);

  • @tankstechtutorials5721
    @tankstechtutorials5721 3 ปีที่แล้ว

    thank you so much for this. great explanation even in 2021

  • @dowonlee7301
    @dowonlee7301 3 ปีที่แล้ว +2

    If you use Node Js verion 15, you should take out ".listen" in line number 4 of server.js file to avoid Syntax Error

  • @Martin-sd6xu
    @Martin-sd6xu 3 ปีที่แล้ว

    Any chances for socket.io tutorial in UIKit ? haha

  • @canybastien8755
    @canybastien8755 3 ปีที่แล้ว +3

    First 4 line of code does not work anymore.
    Replace by:
    var express = require('express');
    var app = express();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);

    • @sukidhardarisi4992
      @sukidhardarisi4992 3 ปีที่แล้ว

      hello, I did the exact same thing in the video, but I can't connect to the server. what am I missing can u help me?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว

      @@sukidhardarisi4992 Please have a look at the pinned comment. If this doesn't fix your comment please provide the error logs you get in your terminal.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว

      @Cany Bastien Thanks for figuring it out. You're right the api changed a bit here since the recording of this video.

  • @shreyanshsingh999
    @shreyanshsingh999 3 ปีที่แล้ว +1

    You are the best

  • @ashleydube3710
    @ashleydube3710 3 ปีที่แล้ว

    Thank you very much

  • @ix901x
    @ix901x ปีที่แล้ว

    Can I use SocketIO with webhook ? or is there any other way to notice the change when webhook get new data ?

  • @MrDe0
    @MrDe0 4 ปีที่แล้ว

    Good job
    is it possible to use another protocol like TCP & UDP?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 ปีที่แล้ว +2

      Thanks! Great question, socket.io is basically a wrapper around WebSockets. WebSockets are TCP based. However, if you are interested in using the UDP protocol you could either use the native API or use the 'SwiftSocket' library, which supports both udp and tcp.

    • @MrDe0
      @MrDe0 4 ปีที่แล้ว +1

      @@BeyondOnesAndZeros ok thank you. 😎

  • @yuvrajagarkar8942
    @yuvrajagarkar8942 3 ปีที่แล้ว

    Why didn’t you do app.listen() why not create a server that way ?

  • @abdullahalnutayfi8103
    @abdullahalnutayfi8103 2 ปีที่แล้ว

    How to build a real database and linke it to this server. For example, I want to upload post and read it (CRUD)

  • @吉崎光
    @吉崎光 3 ปีที่แล้ว +1

    Hello, a really good video saved my time a lot!
    But there is an issue in my environment and I'm having a hard time solving it
    It seems that it's trying to handshake with the node.js server but in the next log it's switching to long polling
    I tested the node.js wsServer connection with javascript and the browser is working just fine so I just can't figure out what's happening.
    The node.js server should console log "connected" when there's a new connection but It isn't logging it in swift either.
    any idea?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว

      Hi, were you able to figure it out? If not you can send me your project and I can have a look at it. Make sure you have the right settings in the Info.plist as suggested in the video, maybe that resolved the issue.

    • @arbenjusuf
      @arbenjusuf 3 ปีที่แล้ว

      @@BeyondOnesAndZeros I am having the same problem. The .connect clientEvent is triggered. But NodeJs app never console.logs on connection event, neither I see any event, when iOS app emits a message. However, when I try the same from the web client, NodeJs console.logs the expected events and data emitted. What can be the problem? I am using socket.io v3 and Socket.IO-Client-Swift 15.2.0

    • @arbenjusuf
      @arbenjusuf 3 ปีที่แล้ว +2

      I found out the problem is related to TypeScript. Socket.io v3.05 still not supported on types yet. I downgraded to socket.io v2.4.x and it works.

    • @吉崎光
      @吉崎光 3 ปีที่แล้ว

      @@arbenjusuf Hi I have already shifted my project to use starscream as the websocket library on the client-side, but nice work.
      In the fact that I also tested the socket.io 2.4 version of the server, it didn't work with the react component as well as the latest version I think I'll use the latest version.
      React client seems to try to reconnect the web socket server over and over.
      Despite that I'm not using the 2.4version of the library, I am curious about why It doesn't work with the React socket.io package.
      Any Idea?

    • @TheDannyDeez
      @TheDannyDeez 3 ปีที่แล้ว

      @@arbenjusuf you are a lifesaver, thank you!

  • @dowonlee7301
    @dowonlee7301 3 ปีที่แล้ว

    how can i send message from ios client to node server? socket.emit is only works inside of Service class,

  • @bronsonlalonde4483
    @bronsonlalonde4483 2 ปีที่แล้ว

    is there a way that I can import the socket.io dependency into a theos project?

  • @kamalCode
    @kamalCode 4 ปีที่แล้ว

    Nice, sir plz nodejs express js next tutorials

  • @diegocamilopenaramirez6101
    @diegocamilopenaramirez6101 3 ปีที่แล้ว

    Thank you, do you have the source code?

  • @arslanilyas4594
    @arslanilyas4594 4 ปีที่แล้ว

    Is this url is same for any project or we can use our own server url
    Like Api or something else

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 ปีที่แล้ว

      It’s the server address from the node.js project that we created, at the beginning of the video. In this example we are running the server locally on our Mac and therefore it is by default localhost + the port defined in the node.js project. However, if you host this node.js project on your own server and run it there you will have to change localhost to the actual server address.

    • @arslanilyas4594
      @arslanilyas4594 4 ปีที่แล้ว

      @@BeyondOnesAndZeros one more thing is port number is necessary for my own server
      Api

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 ปีที่แล้ว

      See the line in the server.js where you set the port to: process.env.PORT || 3000. This line states that it should try to use the port of the server you are hosting it at (for example if you deploy on heroku the port is read from the environment configuration and set) however if there is no such default environment port defined it will use port 3000.

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 ปีที่แล้ว

      So it is necessary to define the port if it is not the default http or https port.

    • @devtech6159
      @devtech6159 3 ปีที่แล้ว

      @@BeyondOnesAndZeros Please share one video to setup the nodejs with nginx proxy. Which should show how to setup nginx as reverse proxy to nodejs app for WebSocket. And using the latest Socket.io library for both ios and nodejs. Please do if possible

  • @twoleggedcat_cs2
    @twoleggedcat_cs2 3 ปีที่แล้ว

    I tried running the app and saw no responses on either the server or the iOS client I did some digging in the log in Xcode and found these errors

    • @twoleggedcat_cs2
      @twoleggedcat_cs2 3 ปีที่แล้ว

      Cant post the errors it seems the Xcode font and text formatting don't agree with TH-cam

    • @twoleggedcat_cs2
      @twoleggedcat_cs2 3 ปีที่แล้ว

      Basically it says error during request

  • @Christine-qx5ce
    @Christine-qx5ce 3 ปีที่แล้ว

    I followed all the steps exactly like in the video but still there is no message coming through. I am getting this console prints: .. Socket connected,... Handling event: statusChange with data [connected, 3] ..... Handling event: connect with data ["/"].... Emitting ["NodeJS Server Port", "Hi Node.JS server"], Ack:false ...
    Can anyone help ? I am really new to Swift and I dont't know what could possible be wrong there. Thanks !

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว +1

      Hi Chris, were you able to resolve your issue? Feel free to contact me with your source code at beyondonesandzeros at gmail dot com and I will see if I can help you with that

    • @Christine-qx5ce
      @Christine-qx5ce 3 ปีที่แล้ว +1

      @@BeyondOnesAndZeros Thanks for the offer. I just send and email but it came back because the adress doesn't exist: i used beyondonesandzero@gmail.com or is there an 's' at the end ?

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว +1

      Sorry you’re right, there is a typo. Should be „zeros“ with an s at the end

    • @Christine-qx5ce
      @Christine-qx5ce 3 ปีที่แล้ว

      @@BeyondOnesAndZeros Thanks I forwarded it there now :)

  • @arslanilyas4594
    @arslanilyas4594 4 ปีที่แล้ว

    Which URL you pass in URL string

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  4 ปีที่แล้ว +1

      I assume you are talking about the localhost url? Please see the answer to the other question :)

  • @CarlosRamirez-bz7ic
    @CarlosRamirez-bz7ic 3 ปีที่แล้ว

    TypeError: require(...).listen is not a function

    • @BeyondOnesAndZeros
      @BeyondOnesAndZeros  3 ปีที่แล้ว +1

      Hi Carlos, please see the pinned comment. The api changed with the latest version of socket.io